No don't do that. It might make the program slower depending on its data. If it ever gets to the point where the processing time spent on checking tool tips justifies spending time on it to improve it over some other aspect of your program, then you might want to try something else but probably not that method. Sometimes bruteforcing is faster because technology has changed; memory access time hasn't kept up with processor speeds. (That's why arrays are better than linked-lists and grids are better than spatial trees.)
If you absolutely had to improve on the efficiency test (for hit testing tons of in-game objects maybe) you would try something different. You would either share the rectangles with the GPU or other threads and attack the problem in parallel. Or you would develop a way to "instantly" determine which rectangle to jump to (similar to a random access collection or hash table). For example, you might look at that 3x4 grid and decide you store its strings in a 12 element array instead of 12 random objects.
1 2 3
| int x = (mouseX - left) / cellWidth; int y = (mouseY - top) / cellHeight; if(x >= 0 && x < 4 && y >= 0 && y < 3) { toolTipText = texts[y * 4 + x]; } |
However, you would only want to invest your personal time in doing this if you had a
lot of rectangles. Less than a thousand? Probably not going to make a meaningful dent in your execution time.
And lets not forget, tool tips don't pop up the instant your put your mouse over them, or else you would see a lot of flickering boxes on your screen while moving your mouse around. It's not an aspect of a game that needs to be optimized and even if it were slow, then you could span the look up over several game updates if you absolutely had to. (In another low priority thread even.)