Three major programming languages: Java, C/C ++, and Ruby

Source: Internet
Author: User
You may think the following chart is interesting, because it is a quality data filtering algorithm written in three programming languages (Ruby, Java, C/C ++: sieve of Eratosthenes)

Well, obviously Ruby is slow, and it is about 1.5 orders of magnitude slower (Note: about 30 times ). This is not good news for Ruby enthusiasts. But from another perspective, call!
Ruby is as fast as top computers five or six years ago. Do you remember the first time you ran a program on a machine with a clock cycle of less than 1 MHz!

Note that these three curves are in the same shape. We can learn from the previous blog why the curves are linear. The last thing to note is that the Java curve is faster than C ++ with extremely small advantages. You
It can be complained that it is because the GCC compiler is not used to optimize the compilation (I am using cygwin). However, if there is any
C ++ programmers will laugh at Java's performance. I advise you to reload the volume of attention.

For Java programmers whose development environment is 30 times faster than Ruby, I am sure the ruby real-time compiler is coming soon. In any case, I prefer clean, concise, and easy-to-maintain code as it is 1.5 orders of magnitude faster.

Ruby

Require 'benchmark'
Def sieveperformance (N)
R = benchmark. realtime () Do
Sieve = array. New (n, true)
Sieve [0 .. 1] = [false, false]

2. upto (INTEGER (math. SQRT (N) do | I |
If sieve [I]
(2 * I). Step (n, I) Do | j |
Sieve [J] = false
End
End
End
End
R
End
Java

Public class generateprimes {
Public static double generate (INT max ){
Long start = system. currenttimemillis ();
Boolean sieve [] = new Boolean [Max];
Arrays. Fill (sieve, true );
Sieve [0] = false;
Sieve [1] = false;
For (INT I = 2; I <math. SQRT (max); I ++ ){
If (sieve [I]) {
For (Int J = 2 * I; j <sieve. length; j + = I ){
Sieve [J] = false;
}
}
}
Return (system. currenttimemillis ()-Start)/1000.0;
}
C ++

# Include <iostream>
# Include <math. h>
# Include <sys/time. h>

Using namespace STD;

Double generate (INT max ){
Struct timeval start;
Struct timezone TZ;
Gettimeofday (& START, & Tz );

Bool * sieve = new bool [Max];
For (INT I = 0; I <Max; I ++) sieve [I] = true;
Sieve [0] = false;
Sieve [1] = false;
For (INT n = 2; n <SQRT (max); N ++ ){
If (sieve [N]) {
For (Int J = 2 * n; j <Max; j + = N)
Sieve [J] = false;
}
}

Struct timeval end;
Gettimeofday (& End, & Tz );

Double startsecond = start. TV _usec/1000000.0;
Double endsecond = (end. TV _sec-start. TV _sec) + end. TV _usec/1000000.0;
Return endsecond-startsecond;
}

Int main (int ac, char ** AV ){
For (INT I = 100000; I <= 5000000; I + = 100000 ){
Double time = generate (I );
Cout <time <Endl;
}
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.