Skip to content

Instantly share code, notes, and snippets.

@kindy
Last active February 8, 2019 13:43
Show Gist options
  • Save kindy/7095cd9028996bf0917aafc7fc086db4 to your computer and use it in GitHub Desktop.
Save kindy/7095cd9028996bf0917aafc7fc086db4 to your computer and use it in GitHub Desktop.

Memory Compare

# Lua
1.3KB/Thread

10000   mem:1.28125KB     total:13641.189453125KB

# Luajit
0.5KB/Thread

10000   mem:0.4609375KB   total:5496.4248046875KB

# Fibjs
1.5KB/Fiber

1, 1.0ms 49.152KBytes total: 21.033MBytes
10000, 0.0ms 0.000KBytes total: 36.123MBytes

# Nodejs(V8)
2.3MB/Isolate

126, 3.0ms 2314.240KBytes total: 309.543MBytes
const coroutine = require('coroutine');
const list = [];
let i = 0;
while (i < 10000) {
const s = Date.now();
const ms = process.memoryUsage().rss;
list.push(coroutine.start(function func(v) {
let i = 100;
while (--i) {
coroutine.sleep(10);
}
this.v = v;
}, i));
// parallel
const e = Date.now();
const me = process.memoryUsage().rss;
console.log(`${++i}, ${((e - s)).toFixed(1)}ms ${((me - ms) / 1000).toFixed(3)}KBytes total: ${(me / 1000 / 1000).toFixed(3)}MBytes`);
// await sleep(.05);
}
// console.log(process.memoryUsage().rss / 1024, 'KB')
list.forEach(x => x.join());
console.log('count', list.length, list.slice(0, 3).map(x => `${x.id}, ${x.v}`));
// console.log('count', coroutine.vmid, coroutine.fibers.length, list.length, coroutine.spareFibers, ret.length);
// process.on('beforeExit', exitCode => {
// });
// fb.join();
const ivm = require('isolated-vm');
const sleep = x => new Promise(res => {
setTimeout(res, x * 1000)
});
const list = [];
let i = 0;
(async function() {
while (i < 10000) {
const s = Date.now();
const ms = process.memoryUsage().rss;
list.push(new ivm.Isolate);
const e = Date.now();
const me = process.memoryUsage().rss;
console.log(`${++i}, ${((e - s)).toFixed(1)}ms ${((me - ms) / 1000).toFixed(3)}KBytes total: ${(me / 1000 / 1000).toFixed(3)}MBytes`);
await sleep(.05);
}
})();
print'start'
local time = os.time
local N = 10000
local s = time()
local list = {}
for v = 1, N, 1 do
local ms = collectgarbage('count')
local co = coroutine.create (function() coroutine.yield(v) end)
table.insert(list, co)
local ok, ret = coroutine.resume(co)
local me = collectgarbage('count')
print('' .. ret .. '\tmem:' .. (me - ms) .. '\ttotal:' .. me)
end
local e = time()
print('done' .. '\ttime:' .. (e - s))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment