Skip to content

Instantly share code, notes, and snippets.

@terasakisatoshi
Last active May 1, 2019
Embed
What would you like to do?
Benchmark Julia on Raspberry Pi Zero W
#include <stdio.h>
#include <math.h>
int gcd(int a, int b)
{
while (b != 0)
{
int r = a % b;
a = b;
b = r;
}
return a;
}
int main()
{
int N = 10000;
int s = 0;
for (int a = 1; a <= N; ++a)
{
for (int b = 1; b <= N; ++b)
if (gcd(a,b)==1)
{
s += 1;
}
}
printf("s=%d\n", s);
printf("N=%d\n", N);
double cN = (double)(N);
double approx_pi = sqrt(6*cN*cN /(double)(s));
printf("%lf\n", approx_pi);
}
/*
How to install gcc-8.1.0 on your RaspberryPi series
https://solarianprogrammer.com/2017/12/08/raspberry-pi-raspbian-install-gcc-compile-cpp-17-programs/
```
$ g++ -std=c++17 -O2 coprime.cpp -o cppexe
$ ./cppexe
```
*/
#include <iostream>
#include <numeric>
#include <cmath>
int main()
{
int N = 10000;
int s = 0;
for (int a = 1; a <= N; ++a)
{
for (int b = 1; b <= N; ++b)
//using std::gcd defined in numeric
//https://cpprefjp.github.io/reference/numeric/gcd.html
if (std::gcd(a,b)==1)
{
s += 1;
}
}
printf("s=%d\n", s);
printf("N=%d\n", N);
double cN = static_cast<double>(N);
double approx_pi = std::sqrt(6*cN*cN /static_cast<double>(s));
printf("%lf\n", approx_pi);
}
using Printf
function calc_pi_by_gcd(N)
s = 0
for a in 1:N
for b in 1:N
s += ifelse(gcd(a,b) == 1, 1, 0)
end
s
end
@printf("s=%d\n", s);
@printf("N=%d\n", N);
sqrt(6N^2/s)
end
@terasakisatoshi
Copy link
Author

terasakisatoshi commented Apr 19, 2019

Elapsed time

Julia

Run from terminal.

$ time julia -L coprime.jl -E "N=10000; calc_pi_by_gcd(N)"
s=60794971
N=10000
3.141534239016629

real	0m22.825s
user	0m21.914s
sys	0m0.395s

Initialize Julia REPL and then run calc_pi_by_gcd

               _
   _       _ _(_)_     |  Documentation: https://docs.julialang.org
  (_)     | (_) (_)    |
   _ _   _| |_  __ _   |  Type "?" for help, "]?" for Pkg help.
  | | | | | | |/ _` |  |
  | | |_| | | | (_| |  |  Version 1.1.0 (2019-01-21)
 _/ |\__'_|_|_|\__'_|  |  
|__/                   |

julia> versioninfo()
Julia Version 1.1.0
Commit 80516ca (2019-01-21 21:24 UTC)
Platform Info:
  OS: Linux (arm-linux-gnueabihf)
  CPU: ARMv6-compatible processor rev 7 (v6l)
  WORD_SIZE: 32
  LIBM: libm
  LLVM: libLLVM-6.0.1 (ORCJIT, arm1176jz-s)

julia> include("coprime.jl")
calc_pi_by_gcd (generic function with 1 method)

julia> N=10000
10000

julia> calc_pi_by_gcd(N)
s=60794971
N=10000
3.141534239016629

julia> @time calc_pi_by_gcd(N)
s=60794971
N=10000
 17.658666 seconds (15 allocations: 256 bytes)
3.141534239016629

C

$ gcc --version
gcc (Raspbian 6.3.0-18+rpi1+deb9u1) 6.3.0 20170516
Copyright (C) 2016 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
$ gcc -o cexe -O2 -lm coprime.c
$ time ./cexe
s=60794971
N=10000
3.141534

real	0m45.896s
user	0m44.618s
sys	0m0.069s

$ gcc-8.1.0 --version
gcc-8.1.0 (GCC) 8.1.0
Copyright (C) 2018 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

$ gcc-8.1.0 -o cexe -O2 -lm coprime.c
$ time .cexe
s=60794971
N=10000
3.141534

real	0m47.460s
user	0m46.401s
sys	0m0.032s

C++17

By following the procedure

We can get gcc and g++ of version 8.1.0

$ g++-8.1.0 -std=c++17 -o cppexe -O2 coprime.cpp
$ time ./cppexe
s=60794971
N=10000
3.141534

real	0m54.253s
user	0m52.691s
sys	0m0.040s

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment