@Dodicat
@Richard
I was getting all kinds of different times, so i rewrote the two programs to do an average of 500 loops.
@Dodicat , What averages do you get for the two programs???
MOD Function:
Code: Select all
'====================================================
'Code designed for FreeBASIC compiler , FreeBASIC.net
'Geany IDE with build command set to (FreeBASIC path ) fbc.exe -gen GCC -w all -O 3
'====================================================
'My formula is faster than the computers built in MOD function...
screen 19
randomize
dim as double time1 , time2 , time3 , time4
dim as double builtin_high = 0 , builtin_low = 1
dim as double Albert_high = 0 , Albert_low = 1
dim as longint loops = 0 , loops_to_do = 500
do
loops = 0
builtin_high = 0 : builtin_low = 1
Albert_high = 0 : Albert_low = 1
do
loops+=1
dim as ulongint a = rnd * 1e10
dim as ulongint b = rnd * 1e10
'computers MOD function
dim as ulongint built_in_mod
time1 = timer
for x as longint = 1 to 1e6
built_in_mod = a mod b
next
time2 = timer
if (time2-time1) < builtin_low then builtin_low = (time2-time1)
if (time2-time1) > builtin_high then builtin_high = (time2-time1)
'Alberts MOD formula
dim as ulongint Albert_mod
time3 = timer
for x as longint = 1 to 1e6
Albert_mod = a - ( ( a \ b ) * b )
next
time4 = timer
if (time4-time3) < Albert_low then Albert_low = (time4-time3)
if (time4-time3) > Albert_high then Albert_high = (time4-time3)
dim as longint diff = built_in_mod - Albert_mod
print
print a ; " MOD " ; b
print
print "Built in MOD " ; built_in_mod , time2-time1 , (time2 - time1) / 1e6
print "Albert's MOD " ; Albert_mod , time4-time3 , (time4 - time3) / 1e6
print
print "diff = " ; diff , , "loops = " ; loops ; " out of " ; loops_to_do
if diff <> 0 then print "ERROR" : sleep
if inkey = chr(27) then exit do
loop until loops = loops_to_do '.multikey(1)
print
print "Builtin MOD avg = " ; ( builtin_high + builtin_low ) / 2 / 1e6 , "H = " ; builtin_high / 1e6 , "L = " ; builtin_low / 1e6
print "Albert MOD avg = " ; ( Albert_high + Albert_low ) / 2 / 1e6 , "H = " ; Albert_high / 1e6 , "L = " ; Albert_low / 1e6
print
print "Press ( esc ) to exit..."
sleep
loop until inkey = chr(27)
print
print " Press a key to exit..."
sleep
end
XOR function:
Code: Select all
'====================================================
'Code designed for FreeBASIC compiler , FreeBASIC.net
'Geany IDE with build command set to (FreeBASIC path ) fbc.exe -gen GCC -w all -O 3
'====================================================
'My formula is faster than the computers built in XOR function...My formula works for 1 and all powers of 2 (2^??)
screen 19
randomize
dim as double time1 , time2 , time3 , time4
dim as double builtin_high = 0 , builtin_low = 1
dim as double Albert_high = 0 , Albert_low = 1
dim as longint loops = 0 , loops_to_do = 500
do
loops = 0
builtin_high = 0 : builtin_low = 1
Albert_high = 0 : Albert_low = 1
do
loops+=1
dim as ulongint a = rnd * 1e10
dim as ulongint b = 2 ^ int( rnd *32 )
'computers XOR function
dim as ulongint built_in_xor
time1 = timer
for x as longint = 1 to 1e6
built_in_xor = a xor b
next
time2 = timer
if (time2-time1) < builtin_low then builtin_low = (time2-time1)
if (time2-time1) > builtin_high then builtin_high = (time2-time1)
'Alberts XOR formula ( works for 1 and all powers of 2 )
dim as ulongint Albert_xor
time3 = timer
dim as ulongint md = b+b
dim as ulongint plus = a+b
dim as ulongint minus = a-b
for x as longint = 1 to 1e6
'Albert_xor = ( a mod (b+b) ) + -b
Albert_xor = a - ( ( a \ md ) * md ) ' Albert_MOD formula
if b > Albert_xor then Albert_xor = plus else Albert_xor = minus
next
time4 = timer
if (time4-time3) < Albert_low then Albert_low = (time4-time3)
if (time4-time3) > Albert_high then Albert_high = (time4-time3)
dim as longint diff = built_in_xor - Albert_xor
print
print a ; " XOR " ; b
print
print "Built in XOR " ; built_in_xor , time2-time1 , (time2 - time1) / 1e6
print "Albert's XOR " ; Albert_xor , time4-time3 , (time4 - time3) / 1e6
print
print "diff = " ; diff , , "loops = " ; loops ; " out of " ; loops_to_do
if diff <> 0 then print "ERROR" : sleep
if inkey = chr(27) then exit do
loop until loops = loops_to_do '.multikey(1)
print
print "Builtin XOR avg = " ; ( builtin_high + builtin_low ) / 2 / 1e6 , "H = " ; builtin_high / 1e6 , "L = " ; builtin_low / 1e6
print "Albert XOR avg = " ; ( Albert_high + Albert_low ) / 2 / 1e6 , "H = " ; Albert_high / 1e6 , "L = " ; Albert_low / 1e6
print
print "Press ( esc ) to exit..."
sleep
loop until inkey = chr(27)
print
print " Press a key to exit..."
sleep
end