Skip to content

Instantly share code, notes, and snippets.

@joseph-montanez
Created March 13, 2012 15:44
Show Gist options
  • Save joseph-montanez/2029496 to your computer and use it in GitHub Desktop.
Save joseph-montanez/2029496 to your computer and use it in GitHub Desktop.
Benchmark between NodeJS, PHP, Python, Ringojs and Railo - Object Creation

These benchmarks were done on Windows 7 64 bit, AMD Phenom II X3 720 Process 2.80 GHz, 4 GB RAM

10,000 Objects Created

  • NodeJs 0.6.13 Finished in: 30ms

  • PHP 5.4 Finished in: 37ms

  • Python 3.2.2 Finished in: 70ms

  • Railo 3.3.1 Finished in: 107ms

  • RingoJS 0.8 Finished in 124ms

<cfscript>
import person;
timer label="Finished in" type="inline" {
for (i = 0; i < 100000; i++) {
obj = new Person("Joseph", "Montanez", RandRange(1, 100));
obj.sayHello();
}
}
WriteOutput("<br />");
function PersonNew(string firstName, string lastName, number age) {
person = StructNew();
person.firstName = firstName;
person.lastName = lastName;
person.age = age;
return person;
}
function PersonSayHello(person) {
return "Hello World, I am " & person.firstName & " " & person.lastName & ", and " & person.age & " years old!";
}
timer label="Finished in" type="inline" {
for (i = 0; i < 100000; i++) {
obj = PersonNew("Joseph", "Montanez", RandRange(1, 100));
PersonSayHello(obj);
}
}
</cfscript>
function Person(firstName, lastName, age) {
this.firstName = firstName;
this.lastName = lastName;
this.age = age;
}
Person.prototype.sayHello = function () {
return "Hello World, I am " + this.firstName + " " + this.lastName + ", and " + this.age + " years old!";
}
function PersonNew(firstName, lastName, age) {
return {
firstName: firstName,
lastName: lastName,
age: age,
};
}
function PersonSayHello(person) {
return "Hello World, I am " + person.firstName + " " + person.lastName + ", and " + person.age + " years old!";
}
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/html'});
output = '';
var startTime = new Date();
for (i = 0; i < 100000; i++) {
obj = new Person("Joseph", "Montanez", Math.random() * 100);
output += "<!-- " + obj.sayHello() + " -->";
}
var endTime = new Date();
res.write(output + 'Finished in: ' + (endTime - startTime) + 'ms<br />');
output = '';
var startTime = new Date();
for (i = 0; i < 100000; i++) {
obj = PersonNew("Joseph", "Montanez", Math.random() * 100);
output += "<!-- " + PersonSayHello(obj) + " -->";
}
var endTime = new Date();
res.write(output + 'Finished in: ' + (endTime - startTime) + 'ms');
res.end('');
}).listen(1337, '127.0.0.1');
<?php
class Person {
public $firstName = "";
public $lastName = "";
public $age = 0;
public function __construct($firstName, $lastName, $age) {
$this->firstName = $firstName;
$this->lastName = $lastName;
$this->age = $age;
}
public function sayHello() {
return "Hello World, I am {$this->firstName} {$this->lastName}, and {$this->age} years old!";
}
}
function PersonNew($firstName, $lastName, $age) {
$person = array(
'firstName' => $firstName,
'lastName' => $lastName,
'age' => $age
);
return $person;
}
function PersonSayHello($person) {
return "Hello World, I am {$person['firstName']} {$person['lastName']}, and {$person['age']} years old!";
}
$output = '';
$startTime = microtime(true);
for($i = 0; $i < 100000; $i++) {
$obj = new Person("Joseph", "Montanez", rand(1, 100));
$obj->sayHello();
}
$endTime = microtime(true);
echo $output;
echo 'Finished in: ' . round(($endTime - $startTime) * 1000) . 'ms';
echo '<br />';
$output = '';
$startTime = microtime(true);
for($i = 0; $i < 100000; $i++) {
$obj = PersonNew("Joseph", "Montanez", rand(1, 100));
PersonSayHello($obj);
}
$endTime = microtime(true);
echo $output;
echo 'Finished in: ' . round(($endTime - $startTime) * 1000) . 'ms';
?>
import tornado.ioloop
import tornado.web
import time
import random
class Person():
def __init__(self, firstName, lastName, age):
self.firstName = firstName
self.lastName = lastName
self.age = age
def sayHello(self):
return "Hello World, I am " + self.firstName + " " + self.lastName + ", and " + str(self.age) + " years old!"
def PersonNew(firstName, lastName, age):
return {
'firstName': firstName,
'lastName': lastName,
'age': age
};
def PersonSayHello(obj):
return "Hello World, I am " + obj['firstName'] + " " + obj['lastName'] + ", and " + str(obj['age']) + " years old!"
class MainHandler(tornado.web.RequestHandler):
def get(self):
startTime = int(round(time.time() * 1000))
for x in range(100000):
obj = Person("Joseph", "Montanez", random.randrange(1, 100))
obj.sayHello()
endTime = int(round(time.time() * 1000))
self.write("Finished in: " + str(endTime - startTime) + "ms")
self.write("<br />")
# Faster way?
startTime = int(round(time.time() * 1000))
for x in range(100000):
obj = PersonNew("Joseph", "Montanez", random.randrange(1, 100))
PersonSayHello(obj)
endTime = int(round(time.time() * 1000))
self.write("Finished in: " + str(endTime - startTime) + "ms")
self.write("<br />")
application = tornado.web.Application([
(r"/", MainHandler),
])
if __name__ == "__main__":
application.listen(8888)
tornado.ioloop.IOLoop.instance().start()
<?php
/* Using PHP 5.4 php.exe -S localhost:8000 */
include 'bench.php';
?>
<cfscript>
component {
property number age = 0;
property string firstName = "";
property string lastName = "";
public Person function init(string firstName, string lastName, number age) hint = "Constructor"
{
this.firstName = firstName;
this.lastName = lastName;
this.age = age;
return this;
}
public string function sayHello() hint = "Introduces the person"
{
return "Hello World, I am " & this.firstName & " " & this.lastName & ", and " & this.age & " years old!";
}
}
</cfscript>
var response = require('ringo/jsgi/response');
function Person(firstName, lastName, age) {
this.firstName = firstName;
this.lastName = lastName;
this.age = age;
}
Person.prototype.sayHello = function () {
return "Hello World, I am " + this.firstName + " " + this.lastName + ", and " + this.age + " years old!";
}
function PersonNew(firstName, lastName, age) {
return {
firstName: firstName,
lastName: lastName,
age: age,
};
}
function PersonSayHello(person) {
return "Hello World, I am " + person.firstName + " " + person.lastName + ", and " + person.age + " years old!";
}
exports.index = function (req) {
var startTime = new Date();
for (i = 0; i < 10000; i++) {
obj = new Person("Joseph", "Montanez", Math.random() * 100);
obj.sayHello();
}
var endTime = new Date();
output = 'Finished in: ' + (endTime - startTime) + 'ms<br />';
var startTime = new Date();
for (i = 0; i < 10000; i++) {
obj = PersonNew("Joseph", "Montanez", Math.random() * 100);
output += "<!-- " + PersonSayHello(obj) + " -->";
}
var endTime = new Date();
output += 'Finished in: ' + (endTime - startTime) + 'ms';
return response.html(output);
};
@joseph-montanez
Copy link
Author

Added Node.JS benchmark

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment