arrays - How can I get a solution from this Java Program to solve algorithm? -


for(int=0; i<1,000,000; i++) {      if(data[i]< 0)         data[i]= data[i] * 2;  } 

hello everyone,

i know possible calculate absolute speed of algorithm below if time takes execute memory access 100 nanoseconds , other operations (arithmetic, register accesses, etc.) take 10 nanoseconds? pretty sure problem can used calculate absolute speed. know have 1 of variables ta= 100 nanoseconds, right? rest of missing variables found through lines of code have provided after compile in java program? compiled , ran it, doesn't tell need solve problem. suggestions?

here formula using calculate absolute speed of algorithm:

t=tna x nna + ta x na;   tna= time execute memory nonaccess instruction;  nna= number of memory nonaccess machine language instructions executed;  ta= time execute memory access instruction; na= number of memory access machine language instructions executed; 

here code compile , run see if give of missing variables need solve problem.

@dorakta if concern on classification of operations in the code block. following might starting point.

line 1: for(int=0; i<1,000,000; i++)

i variable in loop, needs memory access , rest operations. executs 1,000,000 times

you have:

1000000 times of  2 - memory access (1 read , 1 assign) 1 - compare 1 - incr (depends. -> 1 read , 1 add) 

line 3: if(data[i]< 0)

data[i] in if statement memory reference , compare operation. executs 1,000,000 times

you have:

1000000 times of  2 - memory access 1 - compare 

line 4: data[i]= data[i] * 2

you have: 0 1000000 times depending on value of data. may want calculate worst case.

hence, 1000000 times. 2 - memory access (1 read , 1 assign) 1 multiply 

hope helps!


Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -