Skip to content

Instantly share code, notes, and snippets.

@joeyw
Created April 20, 2012 16:39
Show Gist options
  • Save joeyw/2430192 to your computer and use it in GitHub Desktop.
Save joeyw/2430192 to your computer and use it in GitHub Desktop.
Shamazing ruby vs java benchmark
package com.wendt.shamazing;
public class App {
public static void main(String[] args) {
String sha = "9c2cddfaedaea9689a22e376aa20191041554fe8";
int ITER = 1000000;
long start = System.nanoTime();
for (int i = 0 ; i < ITER ; i++)
{
Shamazing.getLongestStringInSha(sha);
}
long end = System.nanoTime();
System.out.println(((double)(end - start) / 1000000000.0) + " seconds.");
}
}

Calling Shamazing 1,000,000 times with '9c2cddfaedaea9689a22e376aa20191041554fe8'

ruby 1.9.3-p194

method time
String 10.999024
Integer 11.975492
method user system total real
string 10.740000 0.040000 10.780000 (10.860089)
integer 11.670000 0.040000 11.710000 (11.773810)

Java(TM) SE Runtime Environment (build 1.6.0_31-b04-415-10M3635)

Calling Shamazing 1,000,000 times

First iteration code.

method time
String 20.323546 seconds.
Integer 21.794637 seconds.

After round of refactoring.

method time
String 2.873166 seconds.
Integer 2.817243 seconds.
#!/usr/bin/env ruby
require 'shamazing'
require 'benchmark'
sha = '9c2cddfaedaea9689a22e376aa20191041554fe8'
n = 1000000
start = Time.now
for i in 1..n; Shamazing.string(sha); end
puts "String - #{(Time.now - start)}"
start = Time.now
for i in 1..n; Shamazing.integer(sha); end
puts "Integer - #{(Time.now - start)}"
Benchmark.bm do |x|
x.report("string") { for i in 1..n; Shamazing.string(sha); end }
x.report("integer") { for i in 1..n; Shamazing.integer(sha); end }
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment