Click here to Skip to main content
15,887,746 members
Home / Discussions / Algorithms
   

Algorithms

 
GeneralRe: about search msg. handler in wndproc and addr. in vtable Pin
Luc Pattyn12-Dec-09 1:48
sitebuilderLuc Pattyn12-Dec-09 1:48 
GeneralRe: about search msg. handler in wndproc and addr. in vtable Pin
Richard MacCutchan12-Dec-09 2:40
mveRichard MacCutchan12-Dec-09 2:40 
GeneralRe: about search msg. handler in wndproc and addr. in vtable Pin
Vixelor12-Dec-09 13:41
Vixelor12-Dec-09 13:41 
GeneralRe: about search msg. handler in wndproc and addr. in vtable Pin
Richard MacCutchan12-Dec-09 23:00
mveRichard MacCutchan12-Dec-09 23:00 
GeneralRe: about search msg. handler in wndproc and addr. in vtable Pin
Vixelor12-Dec-09 13:52
Vixelor12-Dec-09 13:52 
GeneralRe: about search msg. handler in wndproc and addr. in vtable Pin
Luc Pattyn12-Dec-09 14:00
sitebuilderLuc Pattyn12-Dec-09 14:00 
GeneralRe: about search msg. handler in wndproc and addr. in vtable Pin
Vixelor12-Dec-09 15:30
Vixelor12-Dec-09 15:30 
GeneralRe: about search msg. handler in wndproc and addr. in vtable Pin
harold aptroot12-Dec-09 23:52
harold aptroot12-Dec-09 23:52 
Note: this post contains mostly speculation and theory, and I'm not in the mood to analyze less-important things such as register access stalls, or even to benchmark anything. But really, that is your task, not mine. It's also quite early in the morning and I just woke up, so there will be mistakes/errors/clear lack of reasoning/etc - use the information in this post at your own risk.

Really?
Let's assume for a minute that all values are equally likely to occur (not true in practice)
Now consider the conditional jumps here, in the sequential version predicting "not found" would be more than 50% accurate for all but the last two entries. In the binary search code, none of the relevant jumps can be predicted at all (every path down the "tree" is equally likely, as assumed).
It depends on your CPU architecture how much this matters, on a Core2 a misprediction costs you 15 cc's. In the binary search way you will have 3.5 mispredictions on average so 52.5 extra cc's due to branch misprediction. In the sequential search code you only need 1 mispredicted branch (the one that ends your search*) giving you 15 extra cc's due to misprediction.
A 36.5 cc's difference doesn't look too bad, but that's only from branch misprediction.
So let's look at the memory access pattern. Suppose the array is not cached (otherwise we'd have little to talk about). In the binary search way, the first couple of accesses will be a cache miss, and in addition to being a cache miss they're also bringing in a piece of data you probably won't need again soon. That sounds as bad as it is. In the sequential scan you're bringing in blocks of data you probably will need, and the CPU will notice your sequential access pattern and start prefetching the next block even before you need it.

* - this sounds lame, since it means putting your most-likely entries near the end would be better than putting them in the beginning to make sure the "not found" prediction isn't changed to "found" by your CPU who's trying to be clever and thereby messing up the upper bound of 1 misprediction. Whether it's better to put those at the beginning and predict "found" depends on how likely it is that the first entries are used compared to the rest. In the binary search we could unbalance the search tree to make the more likely cases faster than the unlikely cases.

And of course, benchmark!
AnswerRe: about search msg. handler in wndproc and addr. in vtable Pin
cp987613-Dec-09 2:26
cp987613-Dec-09 2:26 
QuestionSolving a circularly recursive system of equations [modified] Pin
MikeMarq6-Dec-09 21:37
MikeMarq6-Dec-09 21:37 
AnswerRe: Solving a circularly recursive system of equations Pin
Tim Craig7-Dec-09 14:20
Tim Craig7-Dec-09 14:20 
GeneralRe: Solving a circularly recursive system of equations Pin
MikeMarq7-Dec-09 16:34
MikeMarq7-Dec-09 16:34 
GeneralRe: Solving a circularly recursive system of equations Pin
Tim Craig7-Dec-09 18:10
Tim Craig7-Dec-09 18:10 
GeneralRe: Solving a circularly recursive system of equations Pin
MikeMarq7-Dec-09 18:39
MikeMarq7-Dec-09 18:39 
GeneralRe: Solving a circularly recursive system of equations Pin
Tim Craig7-Dec-09 21:42
Tim Craig7-Dec-09 21:42 
GeneralRe: Solving a circularly recursive system of equations Pin
MikeMarq9-Dec-09 9:52
MikeMarq9-Dec-09 9:52 
GeneralRe: Solving a circularly recursive system of equations Pin
Tim Craig9-Dec-09 13:42
Tim Craig9-Dec-09 13:42 
AnswerRe: Solving a circularly recursive system of equations - I think I may have solved it Pin
MikeMarq7-Dec-09 16:49
MikeMarq7-Dec-09 16:49 
GeneralRe: Solving a circularly recursive system of equations - I think I may have solved it Pin
Gideon Engelberth7-Dec-09 17:18
Gideon Engelberth7-Dec-09 17:18 
GeneralRe: Solving a circularly recursive system of equations - I think I may have solved it Pin
MikeMarq7-Dec-09 18:19
MikeMarq7-Dec-09 18:19 
AnswerRe: Solving a circularly recursive system of equations Pin
mabo429-Dec-09 4:05
mabo429-Dec-09 4:05 
AnswerSuch Equations Don't Exist Pin
Som Shekhar15-Dec-09 7:42
Som Shekhar15-Dec-09 7:42 
QuestionAdvice about choice of algorithm Pin
thebiggestbangtheory25-Nov-09 11:06
thebiggestbangtheory25-Nov-09 11:06 
AnswerRe: Advice about choice of algorithm Pin
Fatbuddha 126-Nov-09 21:02
Fatbuddha 126-Nov-09 21:02 
AnswerRe: Advice about choice of algorithm Pin
Eddy Vluggen27-Nov-09 0:28
professionalEddy Vluggen27-Nov-09 0:28 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.