Click here to Skip to main content
15,887,485 members
Home / Discussions / C#
   

C#

 
GeneralRe: Generic collections Pin
BillWoodruff22-Jul-13 0:44
professionalBillWoodruff22-Jul-13 0:44 
GeneralRe: Generic collections Pin
PozzaVecia22-Jul-13 1:14
PozzaVecia22-Jul-13 1:14 
GeneralRe: Generic collections Pin
harold aptroot22-Jul-13 1:05
harold aptroot22-Jul-13 1:05 
GeneralRe: Generic collections Pin
PozzaVecia22-Jul-13 3:31
PozzaVecia22-Jul-13 3:31 
GeneralRe: Generic collections Pin
BillWoodruff23-Jul-13 2:41
professionalBillWoodruff23-Jul-13 2:41 
GeneralRe: Generic collections Pin
harold aptroot23-Jul-13 3:11
harold aptroot23-Jul-13 3:11 
GeneralRe: Generic collections Pin
BillWoodruff23-Jul-13 4:09
professionalBillWoodruff23-Jul-13 4:09 
GeneralRe: Generic collections Pin
harold aptroot23-Jul-13 5:32
harold aptroot23-Jul-13 5:32 
There aren't many options there.. (there are some, discussed below) I mean, say you have an array and you want to know the index of some value and you have an algorithm that does not potentially look at all elements, how can it know the thing it's trying to find isn't in the part that it hasn't looked at?

Clearly it can't, so in the worst case you're always looking at all elements anyway. Leaving the "standard complexity model" behind for a moment, you can do a lot better than O(n): divide the array in m parts, search each part in parallel, then do a Min-Reduction phase on all the results. That's O(log m) in PRAM (where the assumption m = n is allowed), but PRAM is not a realistic model for present-day CPU computing.
In the CPU world that same algorithm is still O(n) (because it's really O(n/m + log m) and m is a constant now), which doesn't necessarily have to be a bad thing, not all O(n)'s are created equally after all. But this isn't a particularly awesome algorithm:
1) it spends a lot of time just fetching things from memory, and doesn't do a whole lot with them. (of course the normal algorithm does that too, that it will be a worse bottleneck when multiple threads are doing it)
2) even if you abort the threads working on the higher ranges (or if you want any index, not just the lowest: all other threads) when you find the item, you will have wasted work, so even when it's faster it takes more resources.
3) it has a lot of overhead from all the thread business.
In a quick test, this algorithms start pulling ahead on average for an array of a million integers, and then only when the item is not actually in the array (the worst case for the simple linear search).
It almost reaches a speedup of 4x on my machine (which isn't that bad considering it's a quad core + hyperthreading) but each time an item actually is in the array, the multithreaded algorithm loses or almost loses, no matter how big I make the array (tested up to an array of 228 ints).

So it's not really suitable as a default algorithm for libraries: it's terrible in the common case that the item is actually in the array.

modified 23-Jul-13 11:42am.

GeneralRe: Generic collections Pin
BillWoodruff23-Jul-13 18:11
professionalBillWoodruff23-Jul-13 18:11 
GeneralRe: Generic collections Pin
harold aptroot23-Jul-13 22:42
harold aptroot23-Jul-13 22:42 
GeneralRe: Generic collections Pin
harold aptroot21-Jul-13 1:18
harold aptroot21-Jul-13 1:18 
GeneralRe: Generic collections Pin
PozzaVecia21-Jul-13 22:56
PozzaVecia21-Jul-13 22:56 
GeneralRe: Generic collections Pin
BillWoodruff21-Jul-13 23:40
professionalBillWoodruff21-Jul-13 23:40 
GeneralRe: Generic collections Pin
PozzaVecia22-Jul-13 1:18
PozzaVecia22-Jul-13 1:18 
AnswerRe: Generic collections Pin
M.Kamran Asim21-Jul-13 21:19
M.Kamran Asim21-Jul-13 21:19 
QuestionDisable Windows Navigation Pin
aymen Tn18-Jul-13 5:11
aymen Tn18-Jul-13 5:11 
AnswerRe: Disable Windows Navigation Pin
GuyThiebaut18-Jul-13 5:26
professionalGuyThiebaut18-Jul-13 5:26 
AnswerRe: Disable Windows Navigation Pin
Emmanuel Medina18-Jul-13 5:27
professionalEmmanuel Medina18-Jul-13 5:27 
GeneralRe: Disable Windows Navigation Pin
aymen Tn18-Jul-13 22:44
aymen Tn18-Jul-13 22:44 
GeneralRe: Disable Windows Navigation Pin
Freak3018-Jul-13 23:46
Freak3018-Jul-13 23:46 
GeneralRe: Disable Windows Navigation Pin
Eddy Vluggen19-Jul-13 0:26
professionalEddy Vluggen19-Jul-13 0:26 
QuestionHow to save web page as MHTML? Pin
Abdallah Al-Dalleh18-Jul-13 3:25
Abdallah Al-Dalleh18-Jul-13 3:25 
AnswerRe: How to save web page as MHTML? Pin
Emmanuel Medina18-Jul-13 4:00
professionalEmmanuel Medina18-Jul-13 4:00 
AnswerRe: How to save web page as MHTML? Pin
Jay Nardev18-Jul-13 19:21
Jay Nardev18-Jul-13 19:21 
QuestionDynamic Columns in Entity Framework Pin
sengottuvel m18-Jul-13 2:28
sengottuvel m18-Jul-13 2: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.