-3

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?

nicael
  • 18,550
  • 13
  • 57
  • 90
Mehrdad
  • 171
  • 2
  • 8
  • Languages are neither fast nor slow. – phant0m Mar 03 '13 at 10:43
  • OOP does not deny using primitive types. – Mikhail Vladimirov Mar 03 '13 at 10:43
  • 2
    [Microbenchmarks are evil](http://stackoverflow.com/a/2842707/298389) (especially for jvm). I wouldn't be surprised that if you run this java/scala code more times you'll get completely different timings (not necessary faster ones). – om-nom-nom Mar 03 '13 at 10:48
  • 1
    There can not be one particular programming language , that will solve all problems , there is a context for everything. Yes maybe OO languages are slow , but that does not make them useless , maybe computing effeciency is a trade off when it comes to development time . Question is irrelevant and does not met SO standards.... But the stats in the questions are pretty good... – Barath Ravikumar Mar 03 '13 at 10:48
  • 7
    Actually your question may be rephrased as "Why do we have a whole slew of different cars, if we have bugatti veyron which runs pretty fast?" – om-nom-nom Mar 03 '13 at 11:12
  • @om-nom-nom: I liked your second comment! – Mehrdad Mar 03 '13 at 11:19

3 Answers3

5

The primary factor for choosing a language is not always (and even extremely rarely) performance. Which one would you choose between those two:

  1. Language A allows creating a program in 30 days. The response time of every user interaction is 2 milliseconds
  2. Language B allows creating the same program in 2 days. But the response time of every user interaction is 20 milliseconds.

Do you really think the end user care whether the response time is 2 or 20 milliseconds? Those are so tiny times that they're indistinguishable. But 30 days of development compared to 2 days of development is huge. At 1000 dollars a day, it makes quite a difference.

Moreover, your benchmark is flawed.

And moreover, you don't use OOP to be able to use an Integer rather than an int. You use OOP to be able to manipulate complex objects, made of several fields and methods, in a more intuitive way.

om-nom-nom
  • 62,329
  • 13
  • 183
  • 228
JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • @mo-nom-nom that was too fast. – Eng.Fouad Mar 03 '13 at 10:58
  • firstly, I know that my benchmark wasn't perfect, I just wanted my question to have a foundation. secondly, imagine google launches a website which is hit by more than 1 million people per second, here even the difference between 2 and 20 ms makes a huge difference. so do you agree that whenever performance is the key factor, we are some how forced to use c? (e.g. git, opengl, openssl are all written in c). when big projects are written in c, some efficient libraries appear, so why should I not use c libraries which are fast instead of scala libraries for handling complex objects? – Mehrdad Mar 03 '13 at 11:33
  • Also, the speed difference C vs Java/Scala won't be a factor of ten. Do some research on the web. The most commonly known benchmark hints at a factor of like 2. This is for tiny examples with tight loops. In most production applications, the factor will be even less... and that is not even considering how well the application scales out. –  Mar 03 '13 at 12:15
  • @Mehrdad: Most of the applications written are not applications for Google. And Google has enough power to make Java or JavaScript applications run fast enough. BTW, AFAIK, most of their applications are not written in C, but in Java, JavaScript and other higher-level languages. Java is most of the time almost as fast, or even faster than C. But it's much safer, more readable, faster to write and maintain. – JB Nizet Mar 03 '13 at 12:35
1

Hardly a useful benchmark. Chances are that your C and Java compilers immediately optimized the empty loop out, whereas it's not quite as easy in Scala because the Scala version of the loop involves method calls, which would have to be checked for effects first.

Cubic
  • 14,902
  • 5
  • 47
  • 92
  • my question was a bit deeper than my benchmarks. you can search the web for much more better benchmarks; which in almost all of them c is faster than scala. my precise question is why shouldn't we use c libraries (as git is written in c) for handling complex objects and situations instead of scala libraries which are slower? – Mehrdad Mar 03 '13 at 11:39
  • 2
    @Mehrdad hardware is cheap, people are expensive – om-nom-nom Mar 03 '13 at 11:57
  • @om-nom-nom: again liked you comment, but why can I not upvote any comments in here? – Mehrdad Mar 03 '13 at 12:04
1

Because sometimes, often, most of the time, speed of tight loops is the least of your issues.