Take the 2-minute tour ×

I'm training with solving Olympic IT-riddles on one site.

I have provided two solutions:

 - C# 


 - PHP 


I was confused when online judgment showed , that PHP version was faster!!! Why?

C#: 109 ms 3000 Kb

PHP: 45 ms 0 Kb

How could it be?

share|improve this question
add comment

2 Answers

up vote 2 down vote accepted

Your C# version creates three arrays that you don't seem to need. You could replace it with:

string input = Console.ReadLine();
int charCount = input.Distinct().Count();
if(charCount % 2 == 0) ...

The following is probably quicker still:

int charCount = new HashSet<char>(input).Count;
share|improve this answer
add comment

Given the programs given, the execution time of the important bit of the program - finding the unique characters - would definitely not take 109ms. It sounds like whatever "online judgement" is involved is measuring total execution time including process startup, JITting in the case of .NET, etc.

It's a bit like asking which car gets out of a garage faster, and thinking that represents the speed of the car.

Now it's entirely possible that PHP's array_unique function really is very fast, possibly faster than LINQ... but basically you can't get any useful information out of the benchmark results. You should be looking for benchmarks which execute for seconds rather than milliseconds, and which don't include startup/warm-up time, unless that's what you're particularly interested in.

share|improve this answer
Indeed, the "0kB" aspect is fairly impressive too! –  Gabe Nov 25 '12 at 19:12
add comment

Your Answer


