Interview: A question from when I did CS 101…
Look at the following code:
static int OpPerf_0(int x, int y, int z) { return x + y + z; } static int OpPerf_1(int x, int y, int z) { return sum(x,sum(y,z)); } static int sum(int x, int y) { return x + y; }
First estimate the difference of performance between OpPerf_0 and OpPerf_1?
Write out pseudo-machine code on what will be executed with OpPerf_0 and OpPerf_1.
This is a C#, Java, C++, neutral question that tests the person’s understanding of what actually happens when code is executed.
OpPerf_0
LOADADDR OpPerf_O
PUSH ReturnAddress
PUSH X
PUSH Y
PUSH Z
CALL LoadAddress
POP X
POP Y
POP Z
MOVE X, ACC-1
MOVE Y, ACC-0
ADD
MOVE Z, ACC-1
ADD
PUSH ACC-1
CALL ReturnAddress
I will leave OpPerf_1 to the reader….
OpPerf_1
LOADADDR OpPerf_1
PUSH ReturnAddress
PUSH X
PUSH Y
PUSH Z
CALL LoadAddress
POP X
POP Y
POP Z
LOADADDR sum
PUSH X
PUSH Y
PUSH This.Address_1
CALL LoadAddress
POP X
POP Y
MOVE X, ACC-1
MOVE Y, ACC-0
ADD
PUSH ACC-0
CALL This.Address_1
POP TEMP
PUSH This.Address_2
LOADADDR sum
PUSH TEMP
PUSH Z
CALL LoadAddress
POP X
POP Y
MOVE X, ACC-1
MOVE Y, ACC-0
ADD
PUSH ACC-0
CALL This.Address_2
POP TEMP
PUSH TEMP
CALL ReturnAddress
So one is ~ 35 steps and the other is 14 steps, or 250% more steps. Or, a style that instead of needing 10 servers, you need 25 servers….
Comments
Post a Comment