2

Say theoretically you just generate random numbers [using standard libraries] in as many threads as you can [bound by hardware]. How fast would C++ over Java?

no disk i/o, memory or gc. Just pure Math.random() calls across threads.

Robin Green
  • 32,079
  • 16
  • 104
  • 187
user697111
  • 2,232
  • 8
  • 29
  • 40
  • 8
    This is impossible to answer in general. Using what hardware platform, compiler, JVM version, compilation and runtime parameters, ...? And if you have all that defined, IMHO still it is the best to measure it for yourself... – Péter Török Apr 13 '11 at 08:10
  • 3
    A VERY GENERAL speed comparison is that C++ is 10x faster than Java, but can vary greatly. – Cray Apr 13 '11 at 08:12
  • 7
    @Cray, that was true sometime in the (distant) past, but AFAIK not anymore (since years). Regarding pure math functionality, C++ is still said to be noticeably faster, but far from 10x. And in some other areas and scenarios, Java can even outperform C++ by now. See also http://stackoverflow.com/questions/2163411/why-did-java-have-the-reputation-of-being-slow . – Péter Török Apr 13 '11 at 08:13
  • 3
    In my experience: C++ is 2-3x faster. Can be much more if you are able to use SSE/AVX or other hardware specific specialties. – tgmath Apr 13 '11 at 08:14
  • 1
    @Cray, depends on who you ask... – Thorbjørn Ravn Andersen Apr 13 '11 at 08:15
  • 2
    There is no theoretical answer - only ones determined by measurement. I tend to trust the following: http://shootout.alioth.debian.org/u32/which-programming-languages-are-fastest.php?gpp=on&java=on&calc=chart – CurtainDog Apr 13 '11 at 08:16
  • IMHO you can only answer this statistically, http://www.google.co.uk/search?q=java+faster+than+c%2B%2B+benchmark+2011 185K results, http://www.google.co.uk/search?q=c%2B%2B+faster+than+java+benchmark+2011 314K results. – Peter Lawrey Apr 13 '11 at 08:44

3 Answers3

2

Depends on hardware, OS and many other factors... I think this a very hard question to answer, cause there is too many variables involved. Why don't you test it?

Tony The Lion
  • 61,704
  • 67
  • 242
  • 415
2

Test it. And Pay attention to the fact, that Java performance is not static above runtime. Because it can use a hot spot compiler, that compiles the hot spots after it detects them.

Ralph
  • 118,862
  • 56
  • 287
  • 383
1

test both in the environment you will be deploying at.

Imran Omar Bukhsh
  • 7,849
  • 12
  • 59
  • 81
  • 2
    And with realistic programs. It's fairly easy to write benchmarks which "prove" one is better than the other. For both sides. – James Kanze Apr 13 '11 at 08:20
  • Or to repeat a statement I heard a long time ago: Never trust a benchmark you didn't falsify yourself. – James Kanze Apr 13 '11 at 08:21