Programming Religious Wars#
At the end of my 2017 summer internship at Idaho National Laboratory, I performed an experiment to determine the fastest programming language. I had the opportunity to then present my findings at a poster conference.

Code Snippets#
Here are some small code snippets used in my testing:
Vanilla Python#
from decimal import *
precision = 20
getcontext().prec = precision
#n = input("Calculate pi to what accuracy?")
n = 100000
#def f(x):
# return (Decimal('1')-(Decimal(x)**Decimal('2')))**Decimal('0.5')
val = 0
for i in range(n):
i = i+1
val = val+(Decimal('1')-(Decimal(float(i)/float(n))**Decimal('2')))**Decimal('0.5')
val = val*2
pi = (Decimal('2')/n)*(Decimal('1')+val)
print pi
C#
#include <stdio.h>
#include <math.h>
int main()
{
int count, n = 1000000;
double pi;
float val = 0.0, p = 0.5;
for(count = 1; count < n; ++count)
{
val += (pow((1.0-(((float)count/(float)n)*((float)count/(float)n))), p));
}
pi = (val + 1.0) * (2.0/(float)n);
pi = pi*2;
printf("%.11f", pi);
}
Perl#
#!/usr/bin/perl
$n = '1000000';
$n = $n + 0;
$h = $n * 0;
for($a = 1; $a < $n; $a = ++$a){
$t = $a/$n;
$val = (1-(($t)**2))**0.5;
$h+=$val;
}
$h = $h*2;
$pi = (2/$n)*(1+$h);
printf "%.11f", $pi;
Parallel Python#
from mpi4py import MPI
comm = MPI.COMM_WORLD
rank = comm.Get_rank()
size = comm.Get_size()
name = MPI.Get_processor_name()
def f(x):
return (1-(float(x)**2))**float(0.5)
n = 100000000000
nm = dict()
pi = dict()
for i in range(1,size+1):
if i == size:
nm[i] = (i*n/size)+1
else:
nm[i] = i*n/size
if rank == 0:
val = 0
for i in range(0,nm[1]):
val = val+f(float(i)/float(n))
val = val*2
pi[0] = (float(2)/n)*(float(1)+val)
print name, "rank", rank, "calculated", pi[0]
for i in range(1, size):
pi[i] = comm.recv(source=i, tag=i)
number = sum(pi.itervalues())
number = "%.20f" %(number)
import time
time.sleep(0.3)
print "Pi is approximately", number
for proc in range(1, size):
if proc == rank:
val = 0
for i in range(nm[proc]+1,nm[proc+1]):
val = val+f(float(i)/float(n))
val = val*2
pi[proc] = (float(2)/n)*(float(1)+val)
comm.send(pi[proc], dest=0, tag = proc)
print name, "rank", rank, "calculated", pi[proc]
Python Metrics Gathering#
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.ticker import MaxNLocator
import os
import time
tp1 = time.time()
os.system('cd ~/Desktop/Scripts/Practice; ./cpi.pl > piperl.txt')
tp = time.time()-tp1
in_file = open('piperl.txt', 'rt')
perlpi = in_file.read()
in_file.close()
perlpi = float(perlpi)
realpi = 3.14159265358979323846264338
tpy1 = time.time()
os.system('cd ~/Desktop/Scripts/Practice; ./cpi.py > pipy.txt')
tpy = time.time()-tpy1
in_file = open('pipy.txt', 'rt')
pythonpi = in_file.read()
in_file.close()
pythonpi = float(pythonpi)
os.system('cd ~/Desktop/Scripts/Practice; gcc cpi.c')
tc1 = time.time()
os.system('cd ~/Desktop/Scripts/Practice; ./a.out > piinc.txt')
tc = time.time()-tc1
in_file = open('piinc.txt', 'rt')
cpi = in_file.read()
in_file.close()
cpi = float(cpi)
objects = ('Perl', 'Python', 'C')
y_pos = np.arange(len(objects))
performance = [1/tp,1/tpy,1/tc]
precision = [abs(realpi - perlpi),abs(realpi - pythonpi),abs(realpi - cpi)]
plt.figure(1)
plt.subplot(3,1,1)
plt.bar(y_pos, performance, align='center', alpha=0.5)
plt.xticks(y_pos, objects)
plt.ylabel('''Speed
(runs per second)''')
plt.title('CPI Speed and Precision')
plt.subplot(3,1,2)
plt.bar(y_pos, precision, align='center', alpha=0.5)
perlobject = '''Perl
%s''' % perlpi
pyobject = '''Python
%s''' % pythonpi
cobject = '''C
%s''' % cpi
piobjects = [perlobject, pyobject, cobject]
plt.xticks(y_pos, piobjects)
plt.ylabel('''Error
(distance from pi)''')
speed = [tp,tpy,tc]
plt.subplot(3,1,3)
speed = [tp,tpy,tc]
overall = [1/(tp*abs(realpi - perlpi)), 1/(tpy*abs(realpi - pythonpi)), 1/(tc*abs(realpi - cpi))]
plt.bar(y_pos, overall, align='center', alpha=0.5)
plt.xticks(y_pos, objects)
plt.ylabel('''Overall performance
(speed x precision)''')
plt.gcf().subplots_adjust(left=0.18)
plt.gcf().subplots_adjust(bottom=0.15)
plt.savefig('cpistats.pdf',pad_inches=5)
os.system('cd ~/Desktop/Scripts/Practice; rm piperl.txt pipy.txt piinc.txt')
os.system('open cpistats.pdf')
Raspberry Pi Cluster