Skip to content

Instantly share code, notes, and snippets.

@totalgee
Last active February 24, 2022 12:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save totalgee/640805cb8995cf588152 to your computer and use it in GitHub Desktop.
Save totalgee/640805cb8995cf588152 to your computer and use it in GitHub Desktop.
Performance comparison of ChaiScript w/ several other scripting languages
Summary for simple looped calculation (details and code below):
- on OS X 10.10.3, Mac Pro: 3,7 GHz Quad-Core Intel Xeon E5
Language Tested Version Best Time Relative Perf Notes
======== ============== ========= ============= =====
Node.js v0.10.33 0.0028 1
Lua v5.3.0 0.0098 3.5
SuperCollider v3.6.6 0.0138 4.9
Ruby v2.0.0p481 0.0143 5.1
Python v2.7.6 0.0413 14.8
ChaiScript v5.6.0+ (fdcc595) 0.1898 67.8 -DCHAISCRIPT_NO_THREADS -ltcmalloc
ChaiScript v5.6.0+ (ebc6468) 0.2246 80.2 -DCHAISCRIPT_NO_THREADS -ltcmalloc
ChaiScript v5.6.0+ (fdcc595) 0.2493 89.0 -DCHAISCRIPT_NO_THREADS
ChaiScript v5.6.0+ (ebc6468) 0.3325 118.8 -DCHAISCRIPT_NO_THREADS
ChaiScript v5.6.0+ (fdcc595) 0.3599 128.5 -ltcmalloc
ChaiScript v5.6.0+ (fdcc595) 0.4206 150.2
ChaiScript v5.6.0+ (8fc61bf) 0.4949 176.8
////////////////////////////////////////////////////////////
// ChaiScript:
//
// There is a performance unit test that runs this code; you can run it several
// times to get the best run, like this (from the cmake build directory):
// $ (for i in {1..25}; do echo "Testing $i" 1>&2; ctest -R performance -V | grep Elapsed; done) | sort | head -1
//
// now() is implemented in C++ as follows:
//
// double now()
// {
// using namespace std::chrono;
// auto now = high_resolution_clock::now();
// return duration_cast<duration<double>>(now.time_since_epoch()).count();
// }
//
// It is exposed as follows:
//
// mChai.add(chaiscript::fun(&now), "now");
var sum = 0.0
var start = now()
for (var i = 1; i <= 100000; ++i) {
if (i % 2 == 0) {
sum += 1.0 / i;
}
else {
sum += 1.0 / (double(i) * i);
}
}
var end = now()
print("Elapsed time: " + to_string(end - start) + " sum: " + to_string(sum))
//
// ChaiScript v5.6.0+ (8fc61bf) Release (measured April 30, 2015 on Mac Pro: 3,7 GHz Quad-Core Intel Xeon E5):
// Elapsed time: 0.494943 sum: 6.9322
//
// ChaiScript v5.6.0+ (ebc6468) Release (measured April 30, 2015 on Mac Pro: 3,7 GHz Quad-Core Intel Xeon E5):
// (compiled w/ -DCHAISCRIPT_NO_THREADS)
// Elapsed time: 0.332631 sum: 6.9322
//
// ChaiScript v5.6.0+ (ebc6468) Release (measured May 4, 2015 on Mac Pro: 3,7 GHz Quad-Core Intel Xeon E5):
// (compiled w/ -DCHAISCRIPT_NO_THREADS and linked with TCMalloc: -ltcmalloc_minimal)
// (TCMalloc is from gperftools v2.4)
// Elapsed time: 0.224560 sum: 6.9322
//
// ChaiScript v5.6.0+ (fdcc595) Release (measured May 5, 2015 on Mac Pro: 3,7 GHz Quad-Core Intel Xeon E5):
// (compiled w/ -DCHAISCRIPT_NO_THREADS; this commit has a 25% speed improvement compared to ebc6468
// Elapsed time: 0.249339 sum: 6.9322
//
// ChaiScript v5.6.0+ (fdcc595) Release (measured May 5, 2015 on Mac Pro: 3,7 GHz Quad-Core Intel Xeon E5):
// (compiled w/ -DCHAISCRIPT_NO_THREADS and linked with -ltcmalloc_minimal; 15% improvement compared to ebc6468)
// Elapsed time: 0.18984 sum: 6.9322
//
// ChaiScript v5.6.0+ (fdcc595) Release (measured May 5, 2015 on Mac Pro: 3,7 GHz Quad-Core Intel Xeon E5):
// (linked with -ltcmalloc_minimal)
// Elapsed time: 0.359919 sum: 6.9322
//
// ChaiScript v5.6.0+ (fdcc595) Release (measured May 5, 2015 on Mac Pro: 3,7 GHz Quad-Core Intel Xeon E5):
// Elapsed time: 0.420597 sum: 6.9322
////////////////////////////////////////////////////////////
// SuperCollider:
(
var sum = 0.0;
var dur;
dur = {
(1..100000).do { arg i;
if (i % 2 == 0) {
sum = sum + (1.0 / i);
} {
sum = sum + (1.0 / (i * i));
}
};
}.bench(false);
"Elapsed time: % sum: %".format(dur.round(1e-6), sum.round(1e-4)).postln;
""
)
//
// SuperCollider v3.6.6 (measured April 30, 2015 on Mac Pro: 3,7 GHz Quad-Core Intel Xeon E5):
// Elapsed time: 0.013837 sum: 6.9322
////////////////////////////////////////////////////////////
// Python:
from timeit import default_timer as timer
sum = 0.0
start = timer()
for i in range(1, 100001):
if i % 2 == 0:
sum += 1.0 / float(i)
else:
sum += 1.0 / (float(i) * i)
end = timer()
print("Elapsed time: {:.6f} sum: {:.4f}".format(end - start, sum))
//
// Python v2.7.6 (measured April 30, 2015 on Mac Pro: 3,7 GHz Quad-Core Intel Xeon E5):
// Elapsed time: 0.041317 sum: 6.9322
////////////////////////////////////////////////////////////
// Lua:
sum = 0.0
startTime = os.clock()
for i = 1, 100000 do
if i % 2 == 0 then
sum = sum + (1.0 / i)
else
sum = sum + (1.0 / (i * i))
end
end
endTime = os.clock()
print(string.format("Elapsed time: %.6f sum: %.4f", endTime-startTime, sum))
//
// Lua v5.3.0 (measured April 30, 2015 on Mac Pro: 3,7 GHz Quad-Core Intel Xeon E5):
// Elapsed time: 0.009793 sum: 6.9322
////////////////////////////////////////////////////////////
// Node.js (Javascript):
var sum = 0.0;
var start = process.hrtime();
for (var i = 1; i <= 100000; ++i) {
if (i % 2 == 0) {
sum += 1.0 / i;
}
else {
sum += 1.0 / (i * i);
}
}
var elapsed = process.hrtime(start);
var elapsedSecs = elapsed[0] + elapsed[1] * 1e-9;
console.log('Elapsed time: %d sum: %d', elapsedSecs.toFixed(6), sum.toFixed(4));
//
// Node.js v0.10.33 (measured April 30, 2015 on Mac Pro: 3,7 GHz Quad-Core Intel Xeon E5):
// Elapsed time: 0.002819 sum: 6.9322
////////////////////////////////////////////////////////////
// Ruby:
require "benchmark"
sum = 0.0
elapsed = Benchmark.realtime do
for i in (1..100000)
if i % 2 == 0 then
sum = sum + 1.0 / i
else
sum = sum + 1.0 / (i * i)
end
end
end
puts "Elapsed time: %.6f sum: %.4f" % [elapsed, sum]
//
// Ruby v2.0.0p481 (measured May 5, 2015 on Mac Pro: 3,7 GHz Quad-Core Intel Xeon E5):
// Elapsed time: 0.014348 sum: 6.9322
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment