dade wrote:
<Please don't top-post. Put your reply below or interleaved with the
message you are responding to>
another thing:
i like very much using STL and vector in particular but they are pretty slow
and i need to be very fast!
You've got that the wrong way round. Who cares how fast your code is if
it doesn't work.
First, *make it work*, using the simplest and most reliable means
available. In this case that means ditching the 2D array and using a
vector of vectors. Until you have got your code working corrently,
don't even think about efficiency. Not for a moment.
Using simple and reliable tools, you will get your code working
encouragingly soon. Then, and *oly* then, you can consider speed.
When considering speed, it is very tempting to think "Now it's working,
I'm sure I can make it faster. Where shall I start". Or "Vector is a
class so must have some access overhead compared to arrays. I'll
convert my code to use arrays."
Stop yourself immediately if you start thinking like that. It's the
wrong approach.
The question to ask yourself is: "Does my code run sufficiently fast
that the end user will be satisfied?". It might not be as fast as you
think you could make it, but if it's *fast enough* now, who cares
whether it could be faster. You got it working sooner than expected by
using robust tools in your code. It's fast enough. Well done. You've
finished early so take the rest of the week off.
Of course, it might be that your program isn't fast enough. If that's
the case, the important point is that it is *much* easier to speed up
clearly written bug-free code than it is to debug messy prematurely
optimised code.
The approach you've taken has two stages:
1. Write prematurely optimised code.
2. Discover it doesn't work and have a hard time fixing it.
The right approach is:
1. Write clean, correct code
2. *If and only if necessary* speed it up (using a profiler to target
the bottlenecks).
The second approach has two advantages. Firstly, you might find you
don't need to do stage 2 at all. Secondly, if you do have to do it,
stage 2 of the second approach is a lot easier than stage 2 of the
first approach.
Gavin Deane