public
Last active

Simple comparison of Java vs Node.js vs Pythons

  • Download Gist
nodepy.sh
Shell
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
#!/bin/bash
# Run using:
# curl -Lks https://raw.github.com/gist/1543040/nodepy.sh | sh
# Increase the iterations using:
# curl -Lks https://raw.github.com/gist/1543040/nodepy.sh | it=100000000 sh
 
it=${it:-10000000}
echo it=${it} $0
trap 'rm -f pi{,2,3}.{py,java,class,js}' 0
 
cat >pi2.py <<PY
Pi=0.0;
n=1.0;
for i in xrange(${it}):
Pi+=(4.0/n)-(4.0/(n+2.0))
n+=4.0
print("%1.9f" % Pi);
PY
 
cat >pi3.py <<PY
Pi=0.0;
n=1.0;
for i in range(${it}):
Pi+=(4.0/n)-(4.0/(n+2.0))
n+=4.0
print("%1.9f" % Pi);
PY
 
cat >pi.js <<JS
var Pi=0;
var n=1;
for (i=0;i<=${it};i++) {
Pi+=(4/n)-(4/(n+2));
n+=4;
}
console.log(Pi);
JS
 
cat >pi.java <<JAVA
public class pi {
public static void main(String ...args) {
double Pi=0.0;
double n=1.0;
for (int i=0;i<=${it};i++) {
Pi+=(4/n)-(4/(n+2));
n+=4;
}
System.out.println(Pi);
}
}
JAVA
 
for ((py=24; py<=32; py+=2)); do
p=python${py:0:1}.${py:1:1}
which ${p} || continue
${p} -V
time ${p} pi${py:0:1}.py
echo
done
 
for js in "" $@; do
[ -d "${js:-.}" ] || continue
(
export PATH=${js}:${PATH}
which node && {
node -v
time node pi.js
}
)
done
 
which java && which javac && {
javac pi.java && {
java -version
time java -cp . pi
}
}
 
exit 0
z10000000.out
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
it=10000000 sh
/opt/local/bin/python2.4
Python 2.4.6
3.141592604
 
real 0m4.814s
user 0m4.793s
sys 0m0.011s
 
/usr/bin/python2.6
Python 2.6.7
3.141592604
 
real 0m5.237s
user 0m5.211s
sys 0m0.015s
 
/opt/local/bin/python3.2
Python 3.2.2
3.141592604
 
real 0m4.274s
user 0m4.247s
sys 0m0.017s
 
/usr/local/bin/node
v0.6.6
3.141592603588103
 
real 0m0.227s
user 0m0.215s
sys 0m0.011s
/usr/bin/java
/usr/bin/javac
java version "1.6.0_29"
Java(TM) SE Runtime Environment (build 1.6.0_29-b11-402-11M3527)
Java HotSpot(TM) 64-Bit Server VM (build 20.4-b02-402, mixed mode)
3.141592603588103
 
real 0m0.347s
user 0m0.420s
sys 0m0.037s
z100000000.out
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
it=100000000 sh
/opt/local/bin/python2.4
Python 2.4.6
3.141592645
 
real 0m47.771s
user 0m47.673s
sys 0m0.059s
 
/usr/bin/python2.6
Python 2.6.7
3.141592645
 
real 0m49.423s
user 0m49.323s
sys 0m0.059s
 
/opt/local/bin/python3.2
Python 3.2.2
3.141592645
 
real 0m42.737s
user 0m42.648s
sys 0m0.055s
 
/usr/local/bin/node
v0.6.6
3.1415926485894077
 
real 0m1.787s
user 0m1.778s
sys 0m0.017s
/usr/bin/java
/usr/bin/javac
java version "1.6.0_29"
Java(TM) SE Runtime Environment (build 1.6.0_29-b11-402-11M3527)
Java HotSpot(TM) 64-Bit Server VM (build 20.4-b02-402, mixed mode)
3.1415926485894077
 
real 0m1.567s
user 0m1.633s
sys 0m0.037s

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.