QHash search speed using QStrings as keys

I need to draw a dynamic overlay on a QImage . Overlay components are defined in XML and parsed on QHash<QString, QPicture> , where QString is the name (for example, "crosshairs") and QPicture is a QPicture -independent drawing. Then I draw overlay components as they are needed at the position defined at runtime.

Example: I have 10 photos in my QHash that make up every possible element in a HUD. During a certain frame of the video, I need to draw 6 of them in different positions on the image. During the next frame, something changed, and now I just need to draw 4 of them, but 2 of these positions have changed.

Now to my question: if I try to do this quickly, should I redefine my QHash as QHash<int, QPicture> and list the keys to counteract the overhead caused by string comparisons; or comparisons will not affect performance very much? I can easily convert to whole keys, because the XML parser and overlay composer are completely separate classes; but I would like to use a consistent data structure in the application.

Should I overcome my desire for consistency and reuse in order to increase productivity? Will it even be very important if I do this?

+4
source share
2 answers
Gareth, of course, has the correct answer. I would like to expand it a bit.
  • Go for consistency and reuse first. Do not attempt performance bottlenecks; it's hard to hit the balance.
  • Set realistic performance criteria. I assume that you are doing something similar to a game, reasonable criteria would be "support 25 frames per second on my dev machine."
  • Does your app meet the criteria? Yes? Enough optimization, go to 5.
  • Profile your application, optimize the parts that take the most time. Return to 3.
  • Profit!

Let us return to your specific question, if the number of elements in your hash table is less than or about a hundred, the type of key probably does not matter at all.

+6
source

The answer is that you must profile your application. Only if you find that string comparison is a bottleneck should you implement an alternative strategy. Premature optimization is likely to be a waste of time.

First, make sure your program is correct, that is, make sure that it passes all of its unit tests. (I assume that correctness and performance are orthogonal - this is usually a reasonable assumption if you are not programming a tough real-time application). Then, to find out if the performance meets your requirements. Only if the benchmark shows that the performance is too low if you are optimizing, then do it following the instructions of your profiler. Any optimizations you make can be checked for correctness by restarting unit tests.

+5
source

Source: https://habr.com/ru/post/1308025/


All Articles