performance - Why get data from references slower then calculate in c#? -


i test 2 type data behind loop of 65,000,000. different 0.5-0.6 seconds.

test 1:

 ushort e = myfunction(a, b, c);  ushort d = (ushort)(e + (f << 13));  private static ushort myfunction(ushort a, int b, int c) { if (a < (ushort)b)        return 0;  if (a > (ushort)c)       return (ushort)(c - b);  return (ushort)(a - b); 

test 2:

ushort d = arr2dimension[i, j].data; 

data public data member of class type of arr2dimension.

i checked time each 1 of test (separately) 3 times, first test faster around 0.5-0.6 seconds.

why that? (why data reference slower?)

hard give accurate answer when don't post code can try run ourselves. yes, there pattern code this, discovered accessing memory 1 of slowest things processor can do.

it problem related distance, further electronic circuit physically removed, slower signal must ensure doesn't corrupted. basic principle of "there no free lunch" in electronic engineering. , why new processors use smaller feature sizes in silicon automatically faster.

your myfunction() method uses processor resources close together. a, b , c variables , method return value stored in registers, small storage location very close processor's execution engine. close can be. expectation code runs @ full bore, executing several instructions concurrently each clock cycle. might bogged down bit if() statements, fast if processor has branch prediction data available. depends on how consistently branch taken in previous executions of method. or in other words, how random a, b , c values are.

the array requires accessing circuits not close processor. array element might present in l1 cache, takes 3 cycles. can single cycle if prefetcher guessed @ correctly. when array large speed starts tank when processor can find in l2 or l3 cache. dreadful when has retrieved ram, processor stalled fat 150 cycles waiting ram supply data. distance acting up, can pop case , see wires connect ram processor.


Comments

Popular posts from this blog

node.js - Mongoose: Cast to ObjectId failed for value on newly created object after setting the value -

how does one get csharp-sqlite to throw exceptions for duplicates or foreign key constraint violations -

Simple Angular 2 project fails 'Unexpected reserved word' -