Page 1 of 3 123 LastLast
Results 1 to 10 of 21

Thread: [SOLVED] Why is C slower than Java?

  1. #1
    Join Date
    Nov 2012
    Beans
    23

    [SOLVED] Why is C slower than Java?

    I was always told how C is faster than Java, so I decided to write a few very basic programs to see how much slower it is (if at all) at using basic commands like if, for etc. I compiled my code, and Java was significantly faster (although, as always takes longer to compile). Does Java have some sort of extra stuff built into its for loop so that it can do this more efficiently or something?

    Here's my code...

    Code:
    public class speed {
        public static void main(String[] args) {
             int count;
             for( count = 0; count <= 1000000000; count++ ) {
    
             }
             System.out.print( "\n" +count+ "\n" );
         }
    }
    and for C...
    Code:
    #include <stdio.h>
    
    int main(void)
    {
             int count;
             for( count = 0; count <= 1000000000; count++ ) {
        
             }
             printf( "\n%d\n" ,count );
    
             return 0;
    }
    Also, does anyone know how to indent your code here? (Since I tried, but it ignores the spaces at the start of each line when you post).

    Cheers.
    Last edited by usernamer; March 9th, 2013 at 12:17 AM. Reason: Indent code

  2. #2
    Join Date
    Mar 2006
    Beans
    837

    Re: Why is C slower than Java?

    Probably the printf slows down the output which makes the program to execute slower.

    Put your code inside [ code][ /code] tags or [ php][/ php] tags next time.

  3. #3
    Join Date
    Nov 2012
    Beans
    23

    Re: Why is C slower than Java?

    Cheers for the input, but I doubt it since the printf is outside the for loop, and the C program once compiled takes around 3 seconds to print, and the java one takes around 1/10th of a second

  4. #4
    Join Date
    Nov 2007
    Beans
    Hidden!
    Distro
    Kubuntu

    Re: Why is C slower than Java?

    javac has all optimizations turned on by default whereas gcc doesn't. This means java isn't actually running the loop at all (i.e. it ignores the empty loop).

    If you compile your C program like this:

    Code:
    gcc -O3 -o speed speed.c # -O3 enables all optimizations
    Then it just blows the java program out of the water*

    Either that or give the programs something nontrivial to do while they're inside of the loop so that they don't just skip it (for instance, output the value of count)

    You can look here for more info on gcc optimization flags: http://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html

    *To be fair: java has some additional overhead because it has to load the jvm, for this particular case I expect the speed for both programs should be pretty close. This isn't much of a speed test, really
    Wish I could prove I love you, but does that mean I have to walk on water?
    When we are older you'll understand it's enough when I say so, and maybe some things are that simple.

  5. #5
    Join Date
    Nov 2012
    Beans
    23

    Re: Why is C slower than Java?

    Thanks, I suspected it could be something roughly like that... I did originally have more 0's, but the java compiler complained at me
    Last edited by usernamer; March 9th, 2013 at 01:01 AM.

  6. #6
    Join Date
    Nov 2012
    Beans
    23

    Re: [SOLVED] Why is C slower than Java?

    I did try to output the value of count in the for loop as well before (and again just now with optimisations enabled), and java was still around a second quicker.

  7. #7
    Join Date
    Mar 2006
    Beans
    837

    Re: Why is C slower than Java?

    Quote Originally Posted by usernamer View Post
    Cheers for the input, but I doubt it since the printf is outside the for loop, and the C program once compiled takes around 3 seconds to print, and the java one takes around 1/10th of a second
    Well, I missed that since you hadn't used code tags... (hint php tags highlight syntax regardless of programming language used)

  8. #8
    Join Date
    Feb 2009
    Beans
    1,469

    Re: Why is C slower than Java?

    I for one refuse to use php tags because marking up non-php code with "PHP" violates the structure of the markup. Like using <blockquote> to indent something that's not a quote. I'd use 'em if they were called something different. (Code tags are good though.)

  9. #9
    Join Date
    May 2007
    Location
    Leeds, UK
    Beans
    1,675
    Distro
    Ubuntu

    Re: Why is C slower than Java?

    Java usually benchmarks around 110-120% of the execution time of C and C++ for equivalent code.

    I've got some Eratosthenes sieve programs somewhere that are consistent with this. Exact same algorithm in C, C++, Java, Pascal and Python. Still not a perfect benchmark but more realistic than an empty loop. I'll post them later if you want to play with them.

    Speed isn't everything of course; my Python sieve stumbles in around 20-30 times the execution time of C but it's still a great language and quick enough for most general purpose stuff. A GTK+ program written in Python isn't noticeably sluggish compared to one written in C for example.
    Last edited by r-senior; March 9th, 2013 at 09:42 AM.
    Please create new threads for new questions.
    Please wrap code in code tags using the '#' button or enter it in your post like this: [code]...[/code].

  10. #10
    Join Date
    Jan 2012
    Beans
    123
    Distro
    Ubuntu 13.04 Raring Ringtail

    Re: [SOLVED] Why is C slower than Java?

    C is so faster than java
    java is the slowest programming language i have ever wrote program in

Page 1 of 3 123 LastLast

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •