1. or vs. plus
- Posted by cklester <cklester at yahoo.com> Apr 20, 2006
- 451 views
- Last edited Apr 21, 2006
-- shouldn't these have identical results? -- if not, there's an optimization to be had, right?
include get.e atom timer, c1, c2, x c1=0 c2=0 puts(1,"Timing 'or' vs. '+'\n\n") puts(1,"or: ") timer = time() + 3 while timer > time() do x = 475938 or 837298 c1 += 1 end while ?c1 puts(1,"+ : ") timer = time() + 3 while timer > time() do x = 475938 + 837298 c2 += 1 end while ?c2 if c2 > c1 then printf(1,"\n'+' is %.03f times faster!",{c2/c1}) else printf(1,"\n'or' is %f.2 times faster!",{c1/c2}) end if if wait_key() then end if
-=ck "Programming in a state of Euphoria." http://www.cklester.com/euphoria/
2. Re: or vs. plus
- Posted by cklester <cklester at yahoo.com> Apr 20, 2006
- 445 views
- Last edited Apr 21, 2006
Oops... > > if c2 > c1 then > printf(1,"\n'+' is %.03f times faster!",{c2/c1}) > else > printf(1,"\n'or' is %f.2 times faster!",{c1/c2}) That should be printf(1,"\n'or' is %.03f times faster!",{c1/c2}) -=ck "Programming in a state of Euphoria." http://www.cklester.com/euphoria/
3. Re: or vs. plus
- Posted by Evan Marshall <1evan at sbcglobal.net> Apr 20, 2006
- 447 views
- Last edited Apr 21, 2006
or and + are not the same thing. i.e. 14 + 2 is not equal to 14 or 2 Why should the timing be identical? Or am I missing what you're asking?
4. Re: or vs. plus
- Posted by cklester <cklester at yahoo.com> Apr 21, 2006
- 459 views
Evan Marshall wrote: > > or and + are not the same thing. > > i.e. > 14 + 2 > is not equal to > 14 or 2 > > Why should the timing be identical? Or am I missing what you're asking? I was under the mistaken impression that they were exactly the same! But, I've worked some actual numbers and it seems not so. heheh. oops. :) -=ck "Programming in a state of Euphoria." http://www.cklester.com/euphoria/
5. Re: or vs. plus
- Posted by Derek Parnell <ddparnell at bigpond.com> Apr 21, 2006
- 430 views
cklester wrote: > > Evan Marshall wrote: > > > > or and + are not the same thing. > > > > i.e. > > 14 + 2 > > is not equal to > > 14 or 2 > > > > Why should the timing be identical? Or am I missing what you're asking? > > I was under the mistaken impression that they were exactly the same! But, > I've worked some actual numbers and it seems not so. heheh. oops. :) The 'or' and 'and' operations in the context you are thinking about refer to Binary operations. That is, the operate on the bit values in the number and not the numeric value of the number. These functions below look at the corresponding bits in two values and result in a new bit value fr each corresponding position. or_bits() -- if one or the other bit is 1, then the result is 1 otherwise 0. and_bits() -- if both bits are 1, then the result is 1 otherwise 0. xor_bits() -- if either bit, but not both, are 1 then the result is 1 else 0. So given the numbers 14 and 2, we get the bit patterns 1110 and 0010 respectively. 1110 or 0010 ------ 1110 ( or_bits(14,2) => 14) 1110 and 0010 ------ 0010 ( and_bits(14,2) => 2 ) 1110 xor 0010 ------ 1100 (xor_bits(14,2) => 12) The not_bits() function works with one value and simply reverses the bits. not 1110 ------ 0001 (not_bits(14) => 1) not 0010 ------ 1101 (not_bits(2) => 13) The not_bits() function is often used to 'invert' bits. The or_bits() function is often used to 'turn on' bits. (E.G. in windowing librarys to set flags for controls etc... or_all({WS_CHILD, WS_SYSMENU, WS_BORDER}) The and_bits() function is often used to 'turn off' bits. (E.G. To turn off only the bits in B that are specified in A ... B = and_bits(B, not_bits(A)) ) The xor_bits() function is often used to 'merge' bits. (Special properties of xor are that ... xor(xor(A,B), A) => B xor(xor(A,B), B) => A xor(A,A) => 0 and it can be used to swap two values without using a temporary variable. -- Swap the values of A and B A = xor_bits(A,B) B = xor_bits(A,B) A = xor_bits(A,B) ) -- Derek Parnell Melbourne, Australia Skype name: derek.j.parnell
6. Re: or vs. plus
- Posted by don cole <doncole at pacbell.net> Apr 21, 2006
- 458 views
Hello everybody, In electronics (which should be the same as computers). 1 + 1 = 0 with 1 to carry 1 or 1 = 1 Don Cole