1. [tutor] benchmark: integer float atom
- Posted by _tom (admin) May 25, 2020
- 1151 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
2. Re: [tutor] benchmark: integer float atom
- Posted by petelomax May 25, 2020
- 1120 views
Maybe there is a reason for the Phix results...
Since 42.0, n-1.0, and n-2.0 are all atoms, there is no typecheck for (atom n), however the compiler cannot be sure that the expressions n-1 and n-2 will not ever dip below MININT, so (integer n) emits a fairly quick type check. In some cases, integer n1 = n-1 and the like may help, but I doubt that is the case here. [Note that without typecheck prevents the invocation of udts (user-defined types) and may suppress some builtin tests but is by no means guaranteed to skip them all - in fact doing so would actually slow some things down.] I feel confident other tests will show integer is often quite a bit faster than atom.
Obviously an iterative approach would be much faster, but if you are looking for some truly impressive speed, mpz_fib_ui(res,4784969) completes in 0.1s, and that's a million-digit result! Admittedly generating a million-digit string takes about 2s.
3. Re: [tutor] benchmark: integer float atom
- Posted by Senator May 26, 2020
- 1095 views
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 |
language | integer 42 | float 42.0 | ||
---|---|---|---|---|
Python3 | 126 | 142 | ||
OpenEuphoria | 100 | 118 | ||
Phix | 75.4 | 74.9 | ||
openEuphoria* | 3.6 | 37.14 | * Execution time for Translated/Compiled binary | |
openEuphoria** | 8.96 | 42.5 | **Execution Time + 5.36 Translation/Compile Time | |
Phix/oEu** | 8.4 | 1.8 |