H
Hagen
Hi,
I have a question that you probably shouldn´t worry about since the
compiler cares for it, but anyways:
When you run your compiler with optimization turned on (eg. g++ with -Ox
flag) and your program gets significantly faster than without, did you
write bad code/ have a bad design?
Cause what happens in those optimization steps is, I think, mostly
rearranging of orders. If you had programmed without creating
unnecessary objects and the like it wouldn´t be much left for the
compiler to optimize.
And what is best to think about when trying to improve your code, what
optimization steps cannot be done by the compiler. For example I had a
lot of one line functions. When I inlined those, the resulting
executable got smaller but with no difference in running time or even
running time got worse.
Can you improve your code much by hand if you use STL objects? For this
I have some concrete data:
I have a program that used arrays, for flexibility I changed array to
vector. The vector version was unbelievable slow, so I didn´t access the
elements via [] but changed to iterators. This still was way too slow
and gprof showed that iterator functions consumed most of the time and I
changed to pointers. So here are the numbers for the different versions
compiled via
g++ -o target source.cpp:
array: 48,5 seconds
veciter: 3,5 minutes
vecpoint: 3 minutes
Since someone wrote some time ago that most compilers do a good job
optimizing vector routines I compiled with
g++ -O3 -o target source.cpp:
array: 15 seconds
veciter: 38 seconds
vecpoint: 36 seconds
So did I write initially bad code so that array could be improved more
than 3 times by a compiler? The very bad performance of the vector
versions you can´t blame (fully) on the code since the only change was
array to vector, but how can I try to optimize this code to get the
runtime down to the array version? For this I have to depend on the
compiler optimization I guess (otherwise I should stop programming cause
2+ minutes is a long way to go), but what does the compiler do anyway
and wouldn´t have a effect if I change by hand?
Thanks for any answer, I kind of got carried away from the initial question.
Best wishes, Hagen.
I have a question that you probably shouldn´t worry about since the
compiler cares for it, but anyways:
When you run your compiler with optimization turned on (eg. g++ with -Ox
flag) and your program gets significantly faster than without, did you
write bad code/ have a bad design?
Cause what happens in those optimization steps is, I think, mostly
rearranging of orders. If you had programmed without creating
unnecessary objects and the like it wouldn´t be much left for the
compiler to optimize.
And what is best to think about when trying to improve your code, what
optimization steps cannot be done by the compiler. For example I had a
lot of one line functions. When I inlined those, the resulting
executable got smaller but with no difference in running time or even
running time got worse.
Can you improve your code much by hand if you use STL objects? For this
I have some concrete data:
I have a program that used arrays, for flexibility I changed array to
vector. The vector version was unbelievable slow, so I didn´t access the
elements via [] but changed to iterators. This still was way too slow
and gprof showed that iterator functions consumed most of the time and I
changed to pointers. So here are the numbers for the different versions
compiled via
g++ -o target source.cpp:
array: 48,5 seconds
veciter: 3,5 minutes
vecpoint: 3 minutes
Since someone wrote some time ago that most compilers do a good job
optimizing vector routines I compiled with
g++ -O3 -o target source.cpp:
array: 15 seconds
veciter: 38 seconds
vecpoint: 36 seconds
So did I write initially bad code so that array could be improved more
than 3 times by a compiler? The very bad performance of the vector
versions you can´t blame (fully) on the code since the only change was
array to vector, but how can I try to optimize this code to get the
runtime down to the array version? For this I have to depend on the
compiler optimization I guess (otherwise I should stop programming cause
2+ minutes is a long way to go), but what does the compiler do anyway
and wouldn´t have a effect if I change by hand?
Thanks for any answer, I kind of got carried away from the initial question.
Best wishes, Hagen.