Skip to content

Instantly share code, notes, and snippets.

Created January 21, 2013 23:47
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 anonymous/4590667 to your computer and use it in GitHub Desktop.
Save anonymous/4590667 to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<meta charset="utf-8">
<style>
body {
font: 10px sans-serif;
}
.axis path,
.axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
.line {
fill: none;
stroke: steelblue;
stroke-width: 1.5px;
}
</style>
<body>
<script src="http://d3js.org/d3.v3.js"></script>
<script>
var margin = {top: 20, right: 80, bottom: 30, left: 50},
width = 640 - margin.left - margin.right,
height = 200 - margin.top - margin.bottom;
var x = d3.scale.linear()
.range([0, width]);
var y = d3.scale.linear()
.range([height, 0]);
var color = d3.scale.category10();
var xAxis = d3.svg.axis()
.scale(x)
.ticks(5)
.orient("bottom");
var yAxis = d3.svg.axis()
.scale(y)
.orient("left")
.tickFormat(function(d) {
return d;
});
var line = d3.svg.line()
.x(function(d) { return x(d[0]); })
.y(function(d) { return y(d[1]); });
var svg = d3.select("body").append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
d3.json("new.time.json", function(error, ndata) {
d3.json("module.time.json", function(error, mdata) {
color.domain(['n', 'm']);
x.domain(d3.extent(ndata, function(d) { return d[0]; }));
y.domain([0, d3.max([
d3.max(ndata, function(d) { return d[1]; }),
d3.max(mdata, function(d) { return d[1]; })
])]);
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis);
svg.append("g")
.attr("class", "y axis")
.call(yAxis)
.append("text")
.attr("transform", "rotate(-90)")
.attr("y", 6)
.attr("dy", ".71em")
.style("text-anchor", "end")
.text("Time (ms)");
var city = svg.selectAll(".city")
.data([mdata, ndata])
.enter().append("g")
.attr("class", "city");
city.append("path")
.attr("class", "line")
.attr("d", function(d) { return line(d); })
.style("stroke", function(d) { return color(d); });
});
});
</script>
// module.exports = function instancecounter(c) {
// var cars = [];
// var reads = [];
// for (var i = 0; i < 1000000; i++) {
// cars.push(c());
// if (!(i & (i - 1))) {
// reads.push([i, process.memoryUsage().heapUsed]);
// }
// }
// return reads;
// };
module.exports = function instancecounter(c) {
var cars = [];
var reads = [];
var start = +new Date();
for (var i = 0; i < 1000000; i++) {
cars.push(c());
if (!(i & (i - 1))) {
reads.push([i, +new Date() - start]);
}
}
return reads;
};
function car() {
var c = {},
direction = 0;
c.turn = function(angle) {
direction += angle;
return c;
};
return c;
}
var instancecounter = require('./instancecounter');
console.log(JSON.stringify(instancecounter(car)));
[[0,2243136],[1,2245224],[2,2246112],[4,2246648],[8,2247584],[16,2249320],[32,2253016],[64,2260224],[128,2274296],[256,2304744],[512,2366040],[1024,2485704],[2048,2391480],[4096,2836696],[8192,3730376],[16384,5597432],[32768,8628784],[65536,15793608],[131072,30275448],[262144,60761920],[524288,117828816]]
[[0,1],[1,1],[2,1],[4,1],[8,1],[16,1],[32,1],[64,1],[128,1],[256,1],[512,1],[1024,1],[2048,3],[4096,4],[8192,6],[16384,10],[32768,28],[65536,42],[131072,87],[262144,232],[524288,347]]
function car() {
this.direction = 0;
}
car.prototype.turn = function(angle) {
this.direction += angle;
return this;
};
function makecar() {
return new car();
}
var instancecounter = require('./instancecounter');
console.log(JSON.stringify(instancecounter(makecar)));
[[0,2244016],[1,2245552],[2,2246024],[4,2246352],[8,2247088],[16,2247480],[32,2248488],[64,2250320],[128,2253640],[256,2262584],[512,2278648],[1024,2312296],[2048,2384392],[4096,2107312],[8192,2372032],[16384,2808600],[32768,3303056],[65536,4691000],[131072,8467672],[262144,15341880],[524288,28368176]]
[[0,0],[1,0],[2,0],[4,0],[8,0],[16,0],[32,0],[64,0],[128,0],[256,0],[512,1],[1024,1],[2048,1],[4096,2],[8192,2],[16384,4],[32768,5],[65536,9],[131072,17],[262144,40],[524288,87]]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment