Skip to content

Instantly share code, notes, and snippets.

@nicerobot
Created December 31, 2011 05:47
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nicerobot/1543040 to your computer and use it in GitHub Desktop.
Save nicerobot/1543040 to your computer and use it in GitHub Desktop.
Simple comparison of Java vs Node.js vs Pythons
#!/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
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
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment