Click here to Skip to main content
15,889,216 members
Home / Discussions / C#
   

C#

 
AnswerRe: Code optimization [modified] Pin
DaveyM6918-Oct-09 2:32
professionalDaveyM6918-Oct-09 2:32 
GeneralRe: Code optimization Pin
tvbarnard18-Oct-09 3:10
tvbarnard18-Oct-09 3:10 
GeneralRe: Code optimization Pin
DaveyM6918-Oct-09 3:25
professionalDaveyM6918-Oct-09 3:25 
AnswerRe: Code optimization Pin
Eddy Vluggen18-Oct-09 2:33
professionalEddy Vluggen18-Oct-09 2:33 
GeneralRe: Code optimization Pin
tvbarnard18-Oct-09 3:28
tvbarnard18-Oct-09 3:28 
GeneralRe: Code optimization Pin
Eddy Vluggen18-Oct-09 3:37
professionalEddy Vluggen18-Oct-09 3:37 
GeneralRe: Code optimization Pin
tvbarnard18-Oct-09 3:52
tvbarnard18-Oct-09 3:52 
GeneralRe: Code optimization Pin
Luc Pattyn19-Oct-09 4:21
sitebuilderLuc Pattyn19-Oct-09 4:21 
Hi,

yes it can make a big difference, it is all about cache efficiency.
assume you have two arrays a[] and b[], and you are asked to count how many elements appear in both.

count=0;
foreach(int ia in a) {
    foreach(int ib in b) {
        if (ia==ib) {
            count++;
            break;
    }
}


Lets first assume there aren't many matches, a[] is small (say 1KB) and b[] is large (say 100MB).
So the inner loop will read most of the large array on each outer iteration, hence all of b gets loaded from memory to cache over and over, the cache isn't really working.

Now assume a[] is large (100MB) and b[] is small (1KB); the outer loop works its way through the large array, but does so only once, while the inner loop is getting all of a[] from the cache. This will be faster by several orders of magnitude.

BTW: if you run the above code in a parallized way without taking any precautions, the result may be wrong, as the threads all are operating on the single variable "count". You would have to remedy by:
- locking the variable (bad idea, takes resources and will slow down);
- using Interlocked.Increment()
- or better yet have a counter for each thread, then accumulate them when all have finished

Smile | :)

Luc Pattyn

I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages

Local announcement (Antwerp region): Lange Wapper? 59.24% waren verstandig genoeg om NEEN te stemmen; bye bye viaduct.


GeneralRe: Code optimization Pin
tvbarnard19-Oct-09 5:09
tvbarnard19-Oct-09 5:09 
GeneralRe: Code optimization Pin
Luc Pattyn19-Oct-09 5:37
sitebuilderLuc Pattyn19-Oct-09 5:37 
GeneralRe: Code optimization Pin
tvbarnard19-Oct-09 6:02
tvbarnard19-Oct-09 6:02 
GeneralRe: Code optimization Pin
Luc Pattyn19-Oct-09 6:40
sitebuilderLuc Pattyn19-Oct-09 6:40 
GeneralRe: Code optimization Pin
tvbarnard19-Oct-09 6:55
tvbarnard19-Oct-09 6:55 
GeneralRe: Code optimization Pin
Luc Pattyn19-Oct-09 7:06
sitebuilderLuc Pattyn19-Oct-09 7:06 
GeneralRe: Code optimization Pin
tvbarnard19-Oct-09 7:41
tvbarnard19-Oct-09 7:41 
GeneralRe: Code optimization Pin
Luc Pattyn19-Oct-09 12:45
sitebuilderLuc Pattyn19-Oct-09 12:45 
AnswerRe: Code optimization Pin
tvbarnard20-Oct-09 1:33
tvbarnard20-Oct-09 1:33 
GeneralRe: Code optimization Pin
Luc Pattyn20-Oct-09 1:40
sitebuilderLuc Pattyn20-Oct-09 1:40 
GeneralRe: Code optimization Pin
tvbarnard20-Oct-09 6:50
tvbarnard20-Oct-09 6:50 
GeneralRe: Code optimization Pin
Luc Pattyn20-Oct-09 6:53
sitebuilderLuc Pattyn20-Oct-09 6:53 
GeneralRe: Code optimization Pin
tvbarnard20-Oct-09 6:55
tvbarnard20-Oct-09 6:55 
GeneralRe: Code optimization Pin
Luc Pattyn18-Oct-09 11:24
sitebuilderLuc Pattyn18-Oct-09 11:24 
GeneralRe: Code optimization Pin
tvbarnard18-Oct-09 22:37
tvbarnard18-Oct-09 22:37 
GeneralRe: Code optimization Pin
Luc Pattyn19-Oct-09 4:27
sitebuilderLuc Pattyn19-Oct-09 4:27 
GeneralRe: Code optimization Pin
tvbarnard19-Oct-09 4:42
tvbarnard19-Oct-09 4:42 

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.