Re: Bizarre benchmark result -- C# hundreds of times slower than Java?

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



Jon Skeet [C# MVP] wrote:
Michael A. Covington <look@xxxxxxxxxxxxxxxxxxxxxx> wrote:
While asking some Java enthusiasts what they think about C#, I came across this:

http://www.manageability.org/blog/archive/20030520%23p_the_problem_with_cameron

Reportedly, the (essentially) same program in C# is much, much slower than in Java.

This is a program that is heavy on regexes (which I'm not an expert on) and am wondering if the C# version makes an elementary blunder. Do any experts want to have a look? See also comp.lang.java.

(Query: Is he compiling the regex once in Java, but every time through the loop in C#?)

Looks like he's compiling it every time in both cases - which is stupid on both platforms.

One thing to note is that this test almost certainly says *nothing* about the performance of the languages. Instead, it is a benchmark of the regular expression implementations.

Oh, and it was created in 2003. Worth trying again, preferably having fixed the code to not be braindead.


On my laptop the code never finishes as posted (at least not in the time I allowed it to run), but if I remove the RegexOptions.Compiled, it completes in about 6.5 seconds. I have no idea how the java program would fare on my machine so I won't vouch for its performance compared to the "equivalent" java program.

Let's see what the documentation says about .Compiled:

"Specifies that the regular expression is compiled to an assembly. This yields faster execution but increases startup time. This value should not be assigned to the Options property when calling the CompileToAssembly method."

Ok, so lets check what Pattern.compile does in java:

"Compiles the given regular expression into a pattern."

Hm, not saying much exactly but from docs and all the sources I can find, Pattern.compile(...) is the same as new Regex(...) in .NET.


So in .NET, it is compiled into a binary assembly running the same kind of code as though you built the state machine from scratch, and in java it builds the data structures.

Well, duh, this will take more time. There is no point in compiling the regular expression into an assembly if its to be executed once, like the code posted does.

This is the main problem with most benchmarks that try to prove one language better than another, they invariably fall short on some kind of user problem.

In any case, as Jon pointed out, this isn't a benchmark that tells you anything useful. If anything, it tells you that of the two programs, the java one completed, and the C# one didn't and ran a lot slower to boot. It doesn't say anything about what can be accomplished if you use the platform to its fullest, or at least avoid mistakes like this.

And besides, measuring the performance of C# would be a whole other task. What the program *attempts* to do is measure one part of the BCL against a similar one in Java.

And a rather bad attempt at that.

--
Lasse Vågsæther Karlsen
mailto:lasse@xxxxxxxxxxx
http://presentationmode.blogspot.com/
PGP KeyID: 0xBCDEA2E3
.



Relevant Pages

  • Re: Java on the Apple2 and C64
    ... Java close to the p-machine. ... did many code compaction tunings to the bytecode. ... "discoveries" in computing are simply re-discoveries by folks who either ... One interesting application of compiling a opcode comes from the original Windows EGA/VGA driver. ...
    (comp.sys.apple2)
  • Re: Java on the Apple2 and C64
    ... Java close to the p-machine. ... "discoveries" in computing are simply re-discoveries by folks who either ... One interesting application of compiling a opcode comes from the original Windows EGA/VGA driver. ... All 256 combinations would have overwhelmed the computers of the time, so a routine was compiled on the fly to implement the ROP3. ...
    (comp.sys.apple2)
  • Re: Database applications in Linux
    ... >> Compiled Java code requires a program loader and a set of libraries. ... > uses the loaders and libraries common to those languages, ... Is the JVM part of the OS? ... >> Deploying Java code requires compiling it into object code. ...
    (comp.os.linux.development.apps)
  • RE: Compiling Java on z/OS (Was: why do we need BPXBATCH, JZOS et. al.?)
    ... Subject: Compiling Java on z/OS (Was: why do we need ... McKown, John wrote: ... I agree with you about doing Java ... Sun's code for compiling to byte-codes. ...
    (bit.listserv.ibm-main)
  • Re: Java on the Apple2 and C64
    ... Java close to the p-machine. ... the relatively "looser" Java bytecode. ... compiling", and was originally part of a strategy in which compiled ... "discoveries" in computing are simply re-discoveries by folks who either ...
    (comp.sys.apple2)