代码执行速度:IronPython与C#? [英] Speed of code execution: IronPython vs C#?

查看:58
本文介绍了代码执行速度:IronPython与C#?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正试图从管理者的角度对以下问题做出回应:如果使用IronPython而不是C#编写应用程序,将会导致整体性能下降吗?

Am trying to provide a response from a Managers perspective to the question: What overall performance penalty will we incur if we write the application in IronPython instead of C#?

这个问题针对那些可能已经进行了一些测试,基准测试或已经完成从C#到IronPython的迁移以便量化损失的人.

This question is directed at those who might have already undertaken some testing, benchmarking or have completed a migration from C# to IronPython, in order to quantify the penalty.

推荐答案

我创建了一个C#控制台应用程序(我不太擅长C#), 基本上使用异或密码对输入文件进行加密.

I created a C# console application (I'm not very good at C#), which basically encrypts an input file using a xor cipher.

以下代码:

using System;
using System.IO;
using System.Text;
using System.Diagnostics;

namespace XorCryptor
{
    class Program
    {
        public static void Main(string[] args)
        {
            if(args.Length != 3)
            {
                Console.WriteLine("Usage: xorcryptor password file_in.txt file_out.txt");
                Console.Write("Press any key to continue...");
                Console.ReadKey(true);
                return;
            }
            Stopwatch sw = Stopwatch.StartNew();
            BinaryReader infileb;
            try{
                infileb = new BinaryReader(File.Open(args[1], FileMode.Open));
            }catch(IOException){
                Console.WriteLine("Error reading from input file (is it in use by another program?)");
                return;
            }
            byte[] encb = Crypt(infileb.ReadBytes((int)infileb.BaseStream.Length),args[0]);
            infileb.Close();
            BinaryWriter outfileb;
            try{
                outfileb = new BinaryWriter(File.Open(args[2], FileMode.Create));
            }catch(IOException){
                Console.WriteLine("Error writing to output file (is it in use by another program?)");
                return;
            }
            outfileb.Write(encb);
            outfileb.Close();
            sw.Stop();
            Console.WriteLine("Size: "+encb.Length.ToString());
            Console.WriteLine("Elapsed milliseconds: "+sw.ElapsedMilliseconds.ToString());
        }
        internal static byte[] Crypt(byte[] text, string password)
        {
            int i2 = 0;
            byte[] result = new byte[text.Length];
            foreach(byte b in text)
            {
                result[i2] = Convert.ToByte(b ^ password[i2 % password.Length]);
                i2++;
            }
            return result;
        }
    }
}

然后是等效的Python版本(我使用SharpDevelop 4.0生成可执行文件):

and then the equivalent Python version, (I used SharpDevelop 4.0 to build an executable):

import System
from System.IO import File, FileMode, BinaryReader, BinaryWriter
from System.Diagnostics import Stopwatch
import sys

def crypt(text, password):
    result = System.Array.CreateInstance(System.Byte, text.Length)
    for i, b in enumerate(text):
        result[i] = System.Convert.ToByte(b ^ ord(password[i % len(password)]))
    return result

argv = System.Environment.GetCommandLineArgs()

if len(argv) != 4:
    print "Usage: pyxorcryptor password file_in file_out"
    print "Press any key to exit...",
    System.Console.ReadKey(True)
    sys.exit(1)

sw = Stopwatch.StartNew()
try:
    infileb = BinaryReader(File.Open(argv[2], FileMode.Open))
    outfileb = BinaryWriter(File.Open(argv[3], FileMode.Create))
except IOException, e:
    print e.ToString()
    sys.exit(1)
encb = crypt(infileb.ReadBytes(int(infileb.BaseStream.Length)), argv[1])
infileb.Close()
outfileb.Write(encb)
outfileb.Close()
sw.Stop()

print "Size: {0}\nTime (ms): {1}".format(len(encb), sw.ElapsedMilliseconds)

使用相同的纯文本3,827 kb文件测试其速度, 我明白了:

Testing their speeds with the same plaintext 3,827 kb file, I get this:

C#:

  • 大小:3928779
  • 经过的毫秒数: 73
  • Size: 3928779
  • Elapsed milliseconds: 73

用于.NET 4.0的IronPython 2.6

  • 大小:3928779
  • 时间(毫秒):16825

所以我认为我们可以得出结论,IronPython比C#慢得多, 在这种特定情况下.

So I think we can conclude that IronPython is significantly slower than C#, in this particular scenario.

我可能在这两个文件的代码中的某个地方犯了一个错误,所以请随时指出它们.我还在学习.

I might have made a mistake somewhere in my code in those two files, so feel free to point them out. I'm still learning.

这篇关于代码执行速度:IronPython与C#?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆