WSBT
WSBT

Reputation: 36373

What's the limit of compiler optimization? How smart is it?

People keep telling me instead of writing "shift 1 bit to the left", just write "multiple by 2", because it's a lot more readable, and the compile will be smart enough to do the optimization.

What else would compiles generally do, and developers should not do (for code readability)? I always write string.length == 0 instead of string == "" because I read somewhere 5-6 years ago, saying numeric operations are much faster. Is this still true?

Or, would most compiler be smart enough to convert the following:

int result = 0; for (int i = 0; i <= 100; i++) { result += i; }

into: int result = 5050;?

What is your favourite "optimization" that you do, because most compiles won't do?

Upvotes: 3

Views: 424

Answers (1)

jmkeyes
jmkeyes

Reputation: 3781

Algorithms: no compiler on the planet so far can choose a better algorithm for you. Too many people hastily jump to the rewrite-in-C part after they benchmark, when they should have really considered replacing the algorithm they're using in the first place.

Upvotes: 4

Related Questions