Alex Rivera | Logout

What influences the speed of code?

Asked 2009-03-06T20:19:26.360
9

The way to see how fast your code is going, is performance profiling. There are tools for it and such, but I wonder what the factors are for code speed.

For instance, I've been told that image-editting software will use bitwise operations instead of integer variables to calculate their stuff, simply because it's faster.

So that must mean working with integers and other primitive types takes a few more steps to calculate compared to binairy.

There must be other stuff, but I don't have enough experience with how an OS connects to your hardware and the internal workings of many coding languages to know what.

So I'm asking here: do you know what influences the speed of code?

Not necessarily the speed of programs.

Edit
Report

3 Answers

10

I think you're mistaken. Integers are binary numbers. Image editing software will do anything it can to avoid floating point calculations, because they are incredibly slow compared to integer or bit-shift operations.

But normally, first and foremost you optimize by choosing the right algorithm, not by fiddly little things like worrying about whether to post-increment or pre-increment.

For instance: I just spent the last two days speeding up the way we recalculated a particular set of values. I yanked some stuff out of a loop and pre-calculated it, so it was only done M times instead of M x N times, and stored a value in a variable instead of looking it up each time from somewhere else because that value was used in a Comparator so it would be called a lot during the Collections.sort phase. I got the total execution time from around 45 seconds to 20 seconds. And then one of my coworkers who'd been here a lot longer pointed out that I didn't need to recalculate those values, I could pull them out of a different object. And suddenly it executes in 2 seconds. Now that is optimization I can believe in.

answered 2009-03-06T20:22:53.153
3

The biggest single thing I've run into, on the rare occasions where I've actually cared, is locality. Modern CPUs run very fast, but they have a limited amount of cache memory, which is easily available. When they can't find what they need on cache, they have to read from memory, which is comparatively slow. (When what they're looking for isn't in physical memory, it's really slow.)

Therefore, when it matters, try to keep your code and data compact. Roll up loops as much as possible.

In a multithreaded environment, there's different constraints, and you'll be better off if all your threads are working on data from different cache "lines".

answered 2009-03-06T20:53:48.880
0

What influences the speed of code?

Everything.

Usually the biggest factor is something you never even thought of.

answered 2009-03-06T21:08:21.157

Your Answer