I just made a comparison among Scala, Java and C. Here are the codes I ran and the results I got:
Java (primitive data types):
class test {
public static void main(String args[]) {
long t1=System.nanoTime();
for (int i=0; i<1000000000; i++) {
}
long t2 = System.nanoTime();
System.out.println((t2-t1));
}
}
output: 1181601584
which is 1.2 seconds
C:
#include <stdio.h>
#include <time.h>
int main() {
clock_t t1=clock();
for (int i=0; i<1000000000; i++) {
}
clock_t t2= clock();
printf("%u",(t2-t1)/(double)CLOCKS_PER_SEC);
return 0;
}
output: 2233382994
which is 2.2 seconds
Scala:
object test {
def main(args: Array[String]) {
val nos = 1 to 1000000000
val t1 = System.nanoTime
for (i<-nos) {
}
val t2 = System.nanoTime
println(t2-t1)
}
}
output: 12392282270
which is 12.4 seconds
Java (boxed):
class test {
public static void main(String args[]) {
Long t1=System.nanoTime();
for (Integer i=0; i<1000000000; i++) {
}
Long t2 = System.nanoTime();
System.out.println((t2-t1));
}
}
output: 20756681957
which is 20.8 seconds
Now, my question is, if primitive things are faster that boxed counter-parts, why should we choose to use object-oriented languages? I agree that it is much more easy to write big projects object-orientedly, but if efficiency and speed was what we were looking for (as it is in servers and supercomputers, and also I think a faster software is even better for home users), why would anyone in the world try to write his code in Scala? A deeper question is even why these slow languages have ever appeared?