The Naked Scientists

The Naked Scientists Forum

Author Topic: 01001010100001010100010101.... a question about computer code  (Read 14223 times)

another_someone

  • Guest
01001010100001010100010101.... a question about computer code
« Reply #25 on: 28/01/2008 13:11:29 »
Whichever way it is done, the calculations will still have to be done in the correct order or you will get a wrong answer.

The 'correct' order, yes - but in the last example, some of the instructions could be be reversed and still be correct, because some of the dependencies have been broken by the way it was rewritten.

Looking at your original code:

Code: [Select]
1) $interest = $balance * 0.1;
2) $balance += $interest;
3) $tax = $balance * 0.1;
4) $balance -= $tax;

It could be converted to assembly level code, assuming an abstract machine with at least 5 orthogonal registers, and 3 operand instructions (granted, it is more common for machines to have two operand instructions, but the distinction is not significant for the example to be relevant), the compiler could code your example as:

Code: [Select]
mov r1, $balance   ; [stmt 1 & 2] r1 = balance       
mov r2, 0.1        ; [stmt 1 & 3] r2 = 0.1           
mul r1, r2, r0     ; [stmt 1]     r0 = balance * 0.1
sto r0, $interest  ; [stmt 1]     interest = r0       
add r0, r1, r0     ; [stmt 2]     r0 ($a) = r0 (interest) + r1 (balance)
mul r0, r2, r3     ; [stmt 3]     r3 (tax) = r0 ($a) * r2 (0.1)
sto r3, $tax       ; [stmt 3]     tax = r3                     
sub r0, r3, r3     ; [stmt 4]     r3 (balance) = r0 ($a) - r3 (tax)
sto r3, $balance   ; [stmt 4]     $balance = r3

Which is fairly true to your original code, but it could also code it as:

Code: [Select]
mov r2, 0.1           ; [stmt 1, 3] r2 = 0.1
mov r3, 1.0           ; [stmt 2, 4] r3 = 1.0
mov r1, $balance      ; [stmt 1, 2] r1 = balance
sub r3, r2, r4        ; [stmt 2]    r4 = 1.1
mul r1, r2, r0        ; [stmt 1]    r0 (interest) = r1 (balance) * r2 (0.1)
add r3, r2, r5        ; [stmt 4]    r5 = 0.9
sto r0, $interest     ; [stmt 1]    interest = r0
mul r1, r4, r1        ; [stmt 2]    r1 ($a) = r1 (balance) * r4 (1.1)
mul r1, r5, r0        ; [stmt 4]    r0 (balance) = r1 ($a) * r5 (0.9)
mul r1, r4, r1        ; [stmt 3]    r1 ($tax) = r1 ($a) * r4 (0.1)
sto r0, $balance      ; [stmt 4]    $balance = r0
sto r1, $tax          ; [stmt 3]    $tax = r1

Which interleaves all of the instructions.  This interleaving is not merely arbitrary, since many processors can overlap operations where it sees the result of the previous operation is not required as an input to the next operation.

What you are saying is that any instruction ordering has to honour dependencies in their execuation - which I don't disagree with.  All I am saying is that this has nothing to do with whether it is a banking system or not.  All applications (whether it be a browser trying to work out how to display HTML, or a banking system) has some dependencies, while other bits of code are not interdependent and can safely be reordered by the compiler (and neither the compiler nor the processor is under any obligation to honour your suggested order if it finds it does not violate any interdependencies when the instructions are reordered).
« Last Edit: 28/01/2008 14:18:57 by another_someone »
 

lyner

  • Guest
01001010100001010100010101.... a question about computer code
« Reply #26 on: 28/01/2008 17:09:08 »
Some operations are commutative and some operations are not.
Isn't it as simple as that?
If what you are doing can be written down as a simple set of algebraic expressions and there can be re-written / re-arranged, keeping to the rules,  then you will get the same answer. If the  operations don't follow the rules then you won't.
Any compiler worth its salt will look at the code you have written and re-write it, on your behalf, to make it go as fast as possible - changing the order of things and condensing two or more operations into one, where it can.  Try writing you own benchmark programs and you may find that it takes no longer to do an operation 100 times than it does to do it once; the compiler spotted the redundancy and only ran the routine once.
No interpreter can do that because it only looks at one line at a time.
 

The Naked Scientists Forum

01001010100001010100010101.... a question about computer code
« Reply #26 on: 28/01/2008 17:09:08 »

 

SMF 2.0.10 | SMF © 2015, Simple Machines
SMFAds for Free Forums