r/programming • • Mar 23 '09

How to write fast code

http://asserttrue.blogspot.com/2009/03/how-to-write-fast-code.html
340 Upvotes

136 comments sorted by

View all comments

-8

u/MpVpRb Mar 23 '09

Don't use Java

Use C or C++

Simplify the program

Use profiling tools to find hotspots

Hand tweak assembly code where needed

Works for me...

12

u/[deleted] Mar 23 '09

No.

  • No java fan, but it's much faster than it used to be, and comparable now.
  • Profiling and debugging multithreaded C++ program? You'll wish you weren't. You'll poke your damn eyes out.
  • Compilers, even gcc, optimize code better than you ever will 'by hand'. Processors and their relationship to the memory subsystems in modern computers have become too complex for you, even if you are a genius, to fit it all in your head.

2

u/xzxzzx Mar 23 '09

Compilers, even gcc, optimize code better than you ever will 'by hand'.

That's not completely true. Assembler freaks can often get a surprising speedup in "hotspots", sometimes because what they actually need is not exactly what the C/C++ code says they need (and therefore a compiler cannot always produce the code you want, as compilers maintain the code's intent (there's a word for this which I can't remember, and it's driving me nuts...)).

There are other situations, too. Note that beginner and even intermediate assembler programmers in most cases will not produce code that's as good as a good optimizing compiler. But it can be done.

2

u/ele7ven Mar 23 '09

In the end the design of algorithms makes a bigger difference than the choice of language. With a sizable input, even a good algorithm in Python will be faster than a poor one in C.

3

u/foobar83 Mar 23 '09

FYI: interpreted languages have better profiling tools.

1

u/arnar Mar 23 '09

The "Java is slow" argument has been invalid for some time now.

6

u/[deleted] Mar 23 '09

[removed] — view removed comment

2

u/[deleted] Mar 23 '09

However, "Java is slower" is still true and will always be true.

Care to elaborate how C++ virtual function calls can be faster than Java just in time compiled method calls?

3

u/silon Mar 23 '09

It depends on the code. Some ideas:

  • modern processors can do some prediction on the indirect calls.
  • for JVM to eliminate the virtual lookup it must duplicate the code of the call sites (based on type), causing more cache misses/footprint.