[tutor] benchmark: integer float atom
- Posted by _tom (admin) May 25, 2020
- 1173 views
In ancient times computers did only integer arithmetic and simulated float arithmetic. If you wanted to do efficient float calculations Intel would charge you hundreds extra for this feature.
Tradition has it that 1 was an integer value, but 1.0 was a float value. How meaningful is this today?
Fibonacci calculations require integer values. However, adding decimal points forces values to be float. The results are the same but you can coerse the computer to use two different means of calculating the fibonacci result.
Code for OpenEuphoria or Phix:
function fibonacci_i( integer n)     if n = 0 then         return 0     elsif n = 1 then        return 1     else        return fibonacci_i(n-1) + fibonacci_i(n-2)     end if end function  integer n = 42 ? fibonacci_i( n ) |
function fibonacci_a( atom n )     if n = 0.0 then         return 0.0     elsif n = 1.0 then       return 1.0     else        return fibonacci_a(n-1.0) + fibonacci_a(n-2.0)     end if end function  atom n = 42.0 ? fibonacci_a( n ) |
Code for Python3:
def fibonacci_i(n): Â Â Â Â if n == 0: Â Â Â Â Â Â Â Â return 0 Â Â Â Â elif n == 1: Â Â Â Â Â Â Â Â return 1 Â Â Â Â else: Â Â Â Â Â Â Â Â return fibonacci_i(n-1) + fibonacci_i(n-2) Â print( fibonacci_i(42)) |
def fibonacci_f(n): Â Â Â Â if n == 0.0: Â Â Â Â Â Â Â Â return 0.0 Â Â Â Â elif n == 1.0: Â Â Â Â Â Â Â Â return 1.0 Â Â Â Â else: Â Â Â Â Â Â Â Â return fibonacci_f(n-1.0) + fibonacci_f(n-2.0) Â print( fibonacci_f(42.0) ) |
My typical benchmark program looks like this:
atom t t = time() {} = system_exec( "python3 fib.py" ) ? time() - t
Sample interpreted results for are for fibonacci with both 42 and 42.0 as arguments. Results were identical in all tests. Time was measured in seconds:
language | integer 42 | Â Â Â Â | float 42.0 |
---|---|---|---|
Python3 | 126 | 142 | |
OpenEuphoria | 100 | 118 | |
Phix | 75.4 | 74.9 |
Benchmarking Python produces a scatter of results. OpenEuphoria was consistant. Phix was consistant with a hint that the integer version was a bit slower than the atom version.
- In a conventional language optimizing for integer arithmetic is worthwhile.
- Ignore optimizing in OpenEuphoria and you are still faster than a conventional language.
- Phix is fast; do you want to use your time to chase microsecond improvements?
Maybe there is a reason for the Phix results...
be well
_tom