代码执行速度:IronPython 与 C#?

发布于 2024-09-06 13:08:28 字数 139 浏览 2 评论 0原文

我试图从管理者的角度回答以下问题:如果我们用 IronPython 而不是 C# 编写应用程序,我们会遭受什么总体性能损失?

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

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#?

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

逆光下的微笑 2024-09-13 13:08:28

我创建了一个 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#,
在这个特定的场景中。

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

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

Code below:

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;
        }
    }
}

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)

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


C#:

  • Size: 3928779
  • Elapsed milliseconds:
    73

IronPython 2.6 for .NET 4.0

  • Size: 3928779
  • Time (ms): 16825

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.

若相惜即相离 2024-09-13 13:08:28

IronPython 的性能总是比 C# 慢一点,因为它是解释性语言。您可以在 jcao219 答案 上看到它,尽管它是这不是一个好例子。

但开发速度实际上比性能更重要。对于优秀的 IronPython 开发人员来说,使用 IronPython 开发应用程序比使用 C# 更快。当然,当您(和您的同事)擅长 C# 时,您不应该考虑使用 IronPython 开发应用程序。

整体性能在很大程度上取决于代码的功能。在 IronPython 中加密不是一个好主意。电子表格应用程序是一个好主意 - 请参阅解析器一。他们考虑过将代码从 IronPython 转换为 C#,但他们还没有这样做,因为没有必要。当他们需要加速代码时,他们在 IronPython 本身中找到了一种方法来实现这一点。

IronPython performance will be always a little bit slower than C# because it is interpreted language. You can see it on jcao219 answer although it is not a good example.

But speed of development actually matters more that performance. Developing application in IronPython is faster than in C# for good IronPython developer. Of course, you should not consider developing application in IronPython when you (and your colleagues) are better in C#.

The overall performance heavily depends on what the code do. Encrypting in IronPython is not a good idea. Spreadsheet application is a good idea - see Resolver One. They thought about converting code from IronPython to C# but they have not done it yet because it is not necessary. When they needed to speed up the code, they have found a way in IronPython itself how to do it.

屋顶上的小猫咪 2024-09-13 13:08:28

在单声道环境中,使用有限数量的基准测试,根据 计算机语言基准游戏网站

In the mono environment, using a limited number of benchmarks, IronPython 2.6.1 looks to be 7 to 120 times slower than C# according to the Computer Language Benchmarks Game Site.

↙厌世 2024-09-13 13:08:28

根据我的理解,两种语言都会被编译成 MSIL,因此理论上应用程序的性能应该相同或非常接近,但还有更多的人为因素。

我可以向你保证,我用 C# 编写的程序可能比用 iron-python 编写的相同程序快得多,不是因为 C# 更快,而是因为我更熟悉 C# 并且我知道要做出什么选择,所以如果你的团队如果您更熟悉 C#,那么如果您的应用程序是用 C# 编写的,或者反之亦然,那么您的应用程序将有更好的性能机会。

这只是个人理论,因此请持保留态度。

Based on my understanding both languages will be compiled into MSIL so theoretically the performance of the application should be identical or very close, but there is a lot more to it, the human factor.

I can assure you that a program I write in C# could be a lot faster that the same program in iron-python not because C# is faster but because I'm more familiar with C# and I know what choices to make, so if your team is more familiar with C# your application has a better chance at performance if it is written in C# or the other way around.

This is just a personal theory so take it with a grain of salt.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文