-5

I would like to ask a general question.

I am comparing the run time of sorting on C, Java and Haskell. Which language, provided the same level of optimization (thought that may vary, of course), should come out to have the fastest run time, and which one the slowest (theoretically)? They will all be reading the same text file and sorting words in alphabetical order. If I can get an in depth explanation that would be great, and much appreciated.

Thanks~

3 Answers3

5

There is no "theoretical" answer to this. There is no sound "theory" that will allow you to make accurate predictions.

Besides, the whole idea of a "theoretical" comparison of program language performance is nonsensical. Performance comparisons are about actual (non-theoretical) programs compiled with actual (non-theoretical) compilers running on actual (non-theoretical) machines on actual (non-theoretical) data sets.


If you are asking for a "rule of thumb" guess, based on typical applications, typical compilers, equivalent levels of programmer skill, and as much programmer time as is required, then:

  • C is probably fastest
  • Java is probably 1/3 to 1 times the speed of C
  • Haskell is probably 1/2 to 1 times the speed of Java.

(Based on what the "benchmark game" site says as of 22/2/2015.)

However ... this may be different for some applications, and it depends hugely on the maturity of the compilers and the skills of the programmer in the respective languages.

Besides ... a mature software engineer / project manager does not choose the programming language for a project solely on which one gives the fastest results. Other factors are usually more important than raw speed.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • 1
    Haskell is usually on par with java, performance wise, according to the benchmark games. http://benchmarksgame.alioth.debian.org/u64q/performance.php?test=nbody – gxtaillon Feb 22 '15 at 05:02
  • @gxtaillon - I read it as 1/2 to 1 times the speed. But I've updated my answer anyhow. Thanks. – Stephen C Feb 22 '15 at 05:14
  • 7
    It depends tremendously on the skill of the programmer, and the techniques they choose. It is entirely possible to write a procedure for sorting unboxed mutable vectors in Haskell that's as fast as the best in C. However, most Haskell programmers don't *like* writing that sort of code much, and don't have much use for it anyway. – dfeuer Feb 22 '15 at 05:16
2

Theoretically, they will all be executing the same algorithm, so you are really only dealing with the vagaries of the different languages' implementations, which is a practical -- not a theoretical -- question.

The only way to answer your practical question is to choose concrete implementations (language, compiler, architecture, program, input, etc.) and benchmark them against one another comprehensively.

jschultz410
  • 2,849
  • 14
  • 22
0

Quoting @coobird's answer,

There isn't much that's special about C. That's one of the reasons why it's fast.

Newer languages which have support for garbage collection, dynamic typing and other facilities which make it easier for the programmer to write programs.

The catch is, there is additional processing overhead which will degrade the performance of the application. C doesn't have any of that, which means that there is no overhead, but that means that the programmer needs to be able to allocate memory and free them to prevent memory leaks, and must deal with static typing of variables.

That said, many languages and platforms, such as Java (with its Java Vitual Machine) and .NET (with its Common Language Runtime) have improved performance over the years with advents such as just-in-time compilation which produces native machine code from bytecode to achieve higher performance.

Community
  • 1
  • 1
shauryachats
  • 9,975
  • 4
  • 35
  • 48
  • Yes ... but a properly implemented quicksort (in Java for example) is unlikely to need either garbage collection or dynamic typing. – Stephen C Feb 22 '15 at 04:18