gpt4 book ai didi

ironpython - 代码执行速度 : IronPython vs C#?

转载 作者:行者123 更新时间:2023-12-04 09:43:33 28 4
gpt4 key购买 nike

我试图从经理的角度回答这个问题:如果我们用 IronPython 而不是 C# 编写应用程序,我们会招致什么整体性能损失?

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

最佳答案

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

代码如下:

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 来构建可执行文件):
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 文件测试它们的速度,
我明白了:

C# :
  • 尺码:3928779
  • 经过的毫秒数:
    73


  • IronPython 2.6 for .NET 4.0
  • 尺码:3928779
  • 时间(毫秒):16825


  • 所以我认为我们可以得出结论 IronPython 明显比 C# 慢,
    在这种特殊情况下。

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

    关于ironpython - 代码执行速度 : IronPython vs C#?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3081661/

    28 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com