Code Execution Speed: IronPython vs C #?

I’m trying to give an answer from the point of view of Managers to the question: What kind of overall performance loss will we suffer if we write an application in IronPython instead of C #?

This question is for those who may have already done some testing, benchmarking, or completed the transition from C # to IronPython to quantify the penalty.

+7
ironpython
source share
4 answers

I created a console application in C # (I am not very good at C #), which basically encrypts the input file using the 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 version of Python (I used SharpDevelop 4.0 to create the 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 clear-text file 3.827 kb, I get the following:


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 may have made a mistake in my code in these two files, so feel free to include them. I am still studying.

+12
source share

IronPython's performance will always be slightly slower than C # because it is an interpreted language. You can see it on jcao219's answer , although this is not a very good example.

But development speed is actually more important. Application development in IronPython is faster than in C # for a good IronPython developer. Of course, you should not consider application development in IronPython when you (and your colleagues) are better in C #.

Overall performance is heavily dependent on what the code does. Encryption in IronPython is not a good idea. A spreadsheet application is a good idea - see Resolver One. They thought about converting code from IronPython to C #, but they haven't done it yet, because it's not necessary. When they needed to speed up the code, they found a way in IronPython itself to do it.

+6
source share

In a mono environment, using a limited number of tests, IronPython 2.6.1 looks 7-120 times slower than C # according to Computer language Game console site .

+2
source share

Based on my understanding, both languages ​​will be compiled into MSIL, therefore theoretically the application performance should be the same or very close, but for it there is much more human factor.

I can assure you that the program that I write in C # can be much faster than the same program in python hardware, not because C # is faster, but because I am more familiar with C #, and I know what options to do, so if your team is more familiar with C #, your application has a better chance of performance if it is written in C # or vice versa.

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

-one
source share

All Articles