Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#!/usr/bin/env ksh
#
# Benchmark Project Euler 1 programs by running them through same datsets
# several times and computing the average.
#
Values=(1000 10000 100000 1000000 10000000)
echo "${Values[@]}"
Programs=("./c_euler_1" "./haskell_euler_1")
gcc -O3 -funroll-loops multiples_of_3_and_5.c -o c_euler_1
stack exec ghc -- -O2 -fvectorise multiples_of_3_and_5.hs -o haskell_euler_1
for Program in "${Programs[@]}"
do
for Value in "${Values[@]}"
do
printf "%s %s:\n" "$Program" "$Value"
python -m timeit "__import__('os').system('$Program 0 $Value')" | grep "best of"
done
done
$ ./benchmark.sh
1000 10000 100000 1000000 10000000
./c_euler_1 1000:
100 loops, best of 3: 15.4 msec per loop
./c_euler_1 10000:
100 loops, best of 3: 15.5 msec per loop
./c_euler_1 100000:
100 loops, best of 3: 16.6 msec per loop
./c_euler_1 1000000:
100 loops, best of 3: 17.7 msec per loop
./c_euler_1 10000000:
10 loops, best of 3: 32.9 msec per loop
./haskell_euler_1 1000:
10 loops, best of 3: 73.3 msec per loop
./haskell_euler_1 10000:
10 loops, best of 3: 74.1 msec per loop
./haskell_euler_1 100000:
10 loops, best of 3: 73.9 msec per loop
./haskell_euler_1 1000000:
10 loops, best of 3: 84 msec per loop
./haskell_euler_1 10000000:
10 loops, best of 3: 221 msec per loop
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
int main(int argc, char** argv)
{
if ( argc < 3 )
{
printf("Too few arguments\n");
return 1;
}
uint64_t start = atoi(argv[1]);
uint64_t stop = atoi(argv[2]);
uint64_t sum = 0;
while ( start < stop )
{
if ( start % 3 == 0 || start % 5 == 0 )
{
sum += start;
}
start++;
}
printf("%llu\n", sum);
return 0;
}
import Data.Int
import System.Environment
import System.Exit
sumMultiples :: Int64 -> Int64 -> Int64 -> Int64
sumMultiples start stop sum
| start >= stop = sum
| start `mod` 3 == 0 || start `mod` 5 == 0 = sumMultiples (start + 1) stop (sum + start)
| otherwise = sumMultiples (start + 1) stop sum
main = do
args <- getArgs
if (length args) < 2
then
print "Too few arguments" >> exitFailure
else
print (sumMultiples (read (args!!0) :: Int64) (read (args!!1) :: Int64) 0)
>> exitSuccess
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.