I was creating 1 million invoices like the one below generating random date, random number of items (max 10) and pulling customers and products information out of lists randomly.
It was so fast, I thought there was something wrong with my code. Well there was nothing wrong
with my code JavaScript V8 is just super fast.
This is the JSon of a typical invoice generated once in MongoDB.
{ "InvoiceID" : 5000, "Customer" : { "Name" : "Hot Stuff Foods", "ID" : 22 }, "ArchiveDate" : "Tue May 20 2008 00:00:00 GMT-0400 (Eastern Daylight Time)", "Date" : "Sat Feb 12 2011 00:29:08 GMT-0500 (Eastern Standard Time)", "TotalInvoice" : 188.11, "Status" : "Unknown", "Items" : [ { "Product" : { "Name" : "CHI-CHI'S Con Queso", "Price" : 1.3, "ID" : 2 }, "Price" : 1.3, "Quantity" : 8, "TotalItem" : 10.4 }, { "Product" : { "Name" : "Di Lusso Mediterranean Mixed Olives", "Price" : 8.99, "ID" : 182 }, "Price" : 8.99, "Quantity" : 9, "TotalItem" : 80.91 }, { "Product" : { "Name" : "CHI-CHI'S Fiesta Sweet Corn Cake Mix", "Price" : 8.9, "ID" : 56 }, "Price" : 8.9, "Quantity" : 2, "TotalItem" : 17.8 }, { "Product" : { "Name" : "CHI-CHI'S Fiesta PlatesT Creamy Chipotle Chicken", "Price" : 7.8, "ID" : 12 }, "Price" : 7.8, "Quantity" : 1, "TotalItem" : 7.8 }, { "Product" : { "Name" : "CHI-CHI'S Fiesta Sweet Corn Cake Mix", "Price" : 8.9, "ID" : 56 }, "Price" : 8.9, "Quantity" : 8, "TotalItem" : 71.2 } ] }
So I decided to compare Python, Ruby and JavaScript using Fibonnaci() in iterative mode.
Python
import datetime def Fibonnaci(n): previous = -1 result = 1 for i in range(n+1): sum = result + previous previous = result result = sum return result print "Python %s" % datetime.datetime.now() for t in range(100): for i in range(1000): v = Fibonnaci(i) print "%s" % (datetime.datetime.now())
Ruby
def Fibonnaci(n) previous = -1 result = 1 for i in (1..n) sum = result + previous previous = result result = sum end result end p "Ruby #{Time.now}" for t in (1..100) for i in (1..1000) v = Fibonnaci(i) end end p "#{Time.now}"
JavaScript V8 tested on Node.js
function Fibonnaci(n){ var previous = -1; var result = 1; for(var i=0; i < n+1; i++){ var sum = result + previous; previous = result; result = sum; } return result; } console.log("Node.js "+new Date()); for(var t=0; t < 100; t++){ for(var i=0; i < 1000; i++){ var v = Fibonnaci(i); } } console.log(new Date());
The results
I must be doing something wrong with my Ruby code. I do not understand why it is so slow.
But the speed of execution on JavaScript with V8 is incredible.
Python
2011-02-12 00:51:50
2011-02-12 00:52:03
13s
Ruby
2011-02-12 00:48:38
2011-02-12 00:49:24
46s
Node.js
Sat Feb 12 2011 05:50:50
Sat Feb 12 2011 05:50:50
1s
No comments:
Post a Comment