In Levelfive solutions, we have used so many technologies for our client projects.
Before we choose any technology or a programming language, we always understand the client’s requirements and accordingly choose it. But its mandatory for the programmers to understand the performance of the language well.
This post will show you the performance of Java vs Ruby. For performance, we have used Brute Force Prime Generator and also we have tested the solution across Windows Machine and Linux Machine as well.
Here is the short explanation about the Brute Force,
“A problem solving technique where a series of possible answers are worked out and each possibility is tested for accuracy. This technique is particularly useful on multiple choice problems.”
Here is the ruby code for Brute Force Prime Generator
def BruteForce(topCandidate)
totalCount = 1
isPrime = true
3.step(topCandidate, 2) do |i|
j=3
while j*j < = i && isPrime
isPrime = false if i%j==0
j += 2
end
isPrime ? totalCount += 1 : isPrime = true
end
totalCount
end
max = 1000000
startTime = Time.now()
primes = BruteForce(max)
endTime = Time.now()
elapsed = endTime - startTime
printf("Elapsed time for Brute Force : %f Primes = %dn", elapsed, primes)
and Corresponding Java Code is
package com.perf;
public class PerformanceTest {
public static void main(String[] args) {
long start = System.currentTimeMillis();
int total = bruteForce(1000000);
long end = System.currentTimeMillis();
System.out.println("The total is : " + total +" difference : " + (end-start));
}
public static int bruteForce(long topCandidate) {
int totalCount = 1;
boolean isPrime = true;
for (long i = 3; i < topCandidate; i += 2) {
for (int j = 3; j * j < = i; j += 2) {
if ((i % j) == 0) {
isPrime = false;
break;
}
}
if (isPrime) {
totalCount++;
} else
isPrime = true;
}
return totalCount;
}
}
Here is how performed in Windows.
Sampling | Ruby (In Seconds) | Java (In Seconds) |
1 | 2.292131 | 0.364 |
2 | 2.286131 | 0.366 |
3 | 2.311132 | 0.367 |
Sampling | Ruby (In Seconds) | Java (In Seconds) |
1 | 2.155718 | 0.318 |
2 | 2.158469 | 0.315 |
3 | 2.156114 | 0.315 |
Here is our thoughts on the language
Comparable |
Ruby |
Java |
Typing |
dynamic |
static |
Compile-time type checking |
Static Analysis tools exist |
x |
Runtime type checking |
x |
x |
Constants |
x |
Sort of – final fields |
Interfaces |
x |
x |
Abstract Base Classes |
x |
|
“Everything’s an object.” |
x |
Sort of – native types exist, but have wrappers. |
Operator Overloading |
x |
|
Properties |
x |
|
private members |
x |
|
protected members |
x – public fields are not allowed |
x |
Function Overloading |
x |
|
Optional arguments |
x |
|
Blocks |
x |
|
Interactive Shell |
x |
|
IDEs |
Aptana, Eclipse RDT, Emacs, Vim, Komodo (Edit) |
Eclipse, NetBeans |
Build tools |
rake |
ant, maven |
map/reduce/filter |
x |
|
lambdas |
x – anonymous functions |
|
Closures |
x |
|
Continuations |
x |
|
First-class functions |
x |
|
List Comprehensions |
x |
|
FOSS availability |
good |
good |
Paid software availability |
poor |
good |
Apache Options |
mod_ruby, mod_fastcgi |
tomcat |
DB Support |
x |
x |
Popular frameworks |
Rails and Sinatra |
Numerous |
Scoping |
end keyword |
{ } |
Garbage Collection |
Reference counting |
Generational |
Distribution format |
gems |
jars |
Virtual Environment |
x |
|
immutable strings |
sort of – strings may be “frozen” |
x |
mutable strings |
x |
x |
Boolean |
None evaluates as false. Everything else is true. |
*Only* Boolean-typed values are evaluated as such |
If you like the post, please share your comments.