我试图提供从管理者的角度对问题的回答:如果我们写在IronPython的,而不是C#应用程序,我们将承担什么整体性能损失?
这个问题针对那些可能已经进行过一些测试,基准测试或已经完成从C#迁移到IronPython的人,以量化惩罚.
jca*_*219 12
我创建了一个C#控制台应用程序(我不擅长C#),它基本上使用xor密码加密输入文件.
代码如下:
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;
}
}
}
Run Code Online (Sandbox Code Playgroud)
然后是等效的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)
Run Code Online (Sandbox Code Playgroud)
使用相同的明文3,827 kb文件测试它们的速度,我得到:
C#:
适用于.NET 4.0的IronPython 2.6
所以我认为我们可以得出结论,在这种特殊情况下,IronPython比C#慢得多.
我可能在我的代码中的那两个文件中出错,所以请随意指出它们.我还在学习.
IronPython的性能总是比C#慢一点,因为它是解释语言.你可以在jcao219答案上看到它虽然它不是一个好例子.
但是,发展速度实际上更重要的是表现.对于优秀的IronPython开发人员来说,在IronPython中开发应用程序比在C#中更快.当然,当你(和你的同事)在C#中更好时,你不应该考虑在IronPython中开发应用程序.
整体性能在很大程度上取决于代码的作用.在IronPython中加密并不是一个好主意.电子表格应用程序是个好主意 - 请参阅Resolver One.他们考虑过将代码从IronPython转换为C#,但他们还没有完成,因为它没有必要.当他们需要加速代码时,他们已经在IronPython中找到了一种方法来实现它.
| 归档时间: |
|
| 查看次数: |
7524 次 |
| 最近记录: |