Skip to content

Instantly share code, notes, and snippets.

@zry656565
Created March 14, 2016 16:20
Show Gist options
  • Save zry656565/2aecbab6657e4a058dcc to your computer and use it in GitHub Desktop.
Save zry656565/2aecbab6657e4a058dcc to your computer and use it in GitHub Desktop.
var x,
y,
i,
j,
k,
begin,
end,
len,
sum,
COUNT = 10;
function reset(times) {
x = [], y = []
for (var i = 0; i < times; i++) {
x.push(i)
y.push(2*i)
}
}
var testcases = [1e5, 1e6, 5e6, 1e7, 2e7];
for (i = 0; i < testcases.length; i++) {
sum = 0;
for (k = 0; k < COUNT; k++) {
reset(testcases[i]);
begin = Date.now();
len = x.length;
for (j = 0; j < len; j++) {
y[j] = x[j]
}
end = Date.now();
console.log(end - begin);
sum += end - begin;
}
console.log('test case %d of predefining len: %d ms', testcases[i], sum / COUNT)
sum = 0;
for (k = 0; k < COUNT; k++) {
reset(testcases[i]);
begin = Date.now();
for (j = 0; j < x.length; j++) {
y[j] = x[j]
}
end = Date.now();
console.log(end - begin);
sum += end - begin;
}
console.log('test case %d of not predefining len: %d ms', testcases[i], sum / COUNT)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment