views:

48

answers:

3

I work with many people that program video games for a living. I have a quite a bit of knowledge in C++ and I know a number of general performance strategies to utilize in day to day programming. Like using prefix ++/-- over post fix.

My problem is that often times people come to me to give them tips on general optimizations they can do on a regular basis when programming, but often times these people program in all sorts of languages. Some use C++, C#, Java, ActionScript, etc.

I am wondering if there are any general performance tips that can be utilized on a day by day programming basis? For example, I would suggest prefix ++/-- over postfix for people programming in another language, but I am just not sure if that is true.

My guess is that it is language specific and the best way to go about general optimizations is to make sure you are not using majorly bloated algorithms, but maybe someone has some advice.

A: 

I don't think you can generalize optimization as such. To optimize execution time, you need to dig deep into the language and understand how things work in detail. Just guessing or making assumptions on experiences with other languages won't work! For example, writing x = x << 1 instead of x = x*2 might be a big benefit in C++. In JavaScript it will slow you down.

With all the differences between all the languages it's hard to find generic optimization tips. Maybe for some languages which are similar (f.ex. C# and Java). But if you add both JavaScript and Python to that list I'm pretty sure not many common optimization techniques will be left over.

Also keep in mind that premature optimization is often considered bad practice. Developer-hours are much more expensive than buying additional hardware.

However, there is one thing which comes to mind. Over the past decade or so, Object Relational Mappers have become quite popular. And hence, they emerge(d) in pretty much all popular languages. But you have to be careful with those. It's easy to load tons of data into memory that you will never use in your code if not properly configured. Keep that in mind. Lazy loading might be of some help here. But your mileage will vary.

Optimization depends on so many things that answering such a generic question would make this post explode into a full-fledged paper. In my opinion, optimization should be regarded on a project-by-project basis. Not only Language-by-Language basis.

exhuma
If you have a decent compiler, `x = x << 1;` will be exactly the same as `x = x * 2;`. If you have a compiler so bad that one is faster than the other, the first step in improving performance is to get a better compiler.
David Thornley
Since JavaScript numbers are all floating point, performing `x = x << 1` requires it to convert to an integer, shift left, and convert the result back to a double. Just multiplying by 2 is a simple operation. On a Pentium 4 the shifter was much slower than the adder, so when the compiler translates `x = x * 2` to `x = x + x` it will be an add operation that could be 8 times as fast as the shift.
Gabe
A: 

I think you need to split this into two separate questions:

1) Are there language-agnostic ways to find performance problems? YES. Profile, but avoid the myths around that subject.

2) Are there language-agnostic ways to fix performance problems? IT DEPENDS.

A general language-agnostic principle is: do (1) before you do (2).
In other words, Ready-Aim-Fire, not Ready-Fire-Aim.

Here's an example of performance tuning, in C, but it could be any language.

Mike Dunlavey
+1  A: 

Without going into language specifics, or even knowing whether this is embedded, web, CAD, game, or iPhone programming, there isn't much that can be said. All we know is that there's multiple languages involved, and for some unknown reason performance is always slower than desirable.

First, check your algorithms. A slow algorithm can cause horrible performance. Read up on algorithms and their complexity.

Second, note if there are any really slow operations, such as hitting a database or transmitting information or moving a robot arm. See if the program is doing more of those than it should.

Third, profile. If there's a section of code that's taking 5% of the time, no optimization will make your program more than 5% faster. If a section of code is taking a lot of the time, it's worth looking at.

Fourth, get somebody who knows what they're doing to make any specific optimizations. Test them when they're done to make sure they actually speed up performance. When performance was an issue, I've improved it with some counterintuitive measures, like rolling up loops.

David Thornley