to reverse the sign or an integer variable:
var a:int = -i;
can we perform the same operation faster?
with ~ operator:
var a:int = ~i +1;
with XOR operator
var a:int = (i ^ -1) + 1;
which one is the fastest ?
Unbelievable !
What do you get ?
here the code used to profile:
var i:int; var a:int; var val:int; var time1:uint = getTimer(); for(i= 0;i<5000000;i++) { val = i-2500000; a = -val; a = -val; a = -val; a = -val; a = -val; a = -val; a = -val; a = -val; a = -val; a = -val; } var time2:uint = getTimer(); for(i= 0;i<5000000;i++) { val = i-2500000; a = ~val + 1; a = ~val + 1; a = ~val + 1; a = ~val + 1; a = ~val + 1; a = ~val + 1; a = ~val + 1; a = ~val + 1; a = ~val + 1; a = ~val + 1; } var time3:uint = getTimer(); for(i= 0;i<5000000;i++) { val = i-2500000; a = (val ^ -1) + 1; a = (val ^ -1) + 1; a = (val ^ -1) + 1; a = (val ^ -1) + 1; a = (val ^ -1) + 1; a = (val ^ -1) + 1; a = (val ^ -1) + 1; a = (val ^ -1) + 1; a = (val ^ -1) + 1; a = (val ^ -1) + 1; } var time4:uint = getTimer();
I’m loving these tips. Keep up the good work!
If I could ask for one thing though, could you post your test environment (e.g. CPU, OS, Flash Player version) and the test source code?
Thanks so much for all these posts. They’re fascinating!
Jackson,
Thanks for the nice comment, so I am testing release build of flash 11.7.
I have a PC Intel(R) Core(TM) i7-2720QM CPU@2.2Ghz, 6GB RAM
I now added to each post a test app, so you can see the result by yourself.
And all the source code of everything tested so far is also in that post now :
http://guihaire.com/code/?p=320
(there is a zip with the code I wrote for it, I use Azoth to inject Alchemy opcode).