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.
Performance. IronPython performance is generally comparable to CPython - faster in some regards, slower in others.
IronPython is slower because of DLR (dynamic language runtime) that it is built on.
As a compiled language, C# converts directly into machine code that a processor can execute. No interpreter needed. In some cases, this means that C# code can run up to 44 times faster than Python.
IronPython works as an extension to the . NET Framework, but it can also be used by . NET projects to take advantage of Python's scripting power. Other than that, since IronPython is a real implementation of Python itself, there's no need to learn a new language or extra features if you already know Python.
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#:
IronPython 2.6 for .NET 4.0
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 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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With