boardix


Post New Topic  New Poll  Post A Reply
my profile | directory login | | search | faq | forum home
  next oldest topic   next newest topic
» boardix   » Groups   » Codecrave   » C++ vs. Java speediness.

 - UBBFriend: Email this page to someone!    
Author Topic: C++ vs. Java speediness.
Atomic-Ant
In Real Life Now
Member # 2

 - posted      Profile for Atomic-Ant     Send New Private Message       Edit/Delete Post   Reply With Quote 
Ok, so java's an interpretted language. C++ is a compiled language, and I've heard that C is really fast too... Aye, there's the rub. In doing a simple recursion program to calculate the nth fibonacci number, THE INTERPRETTED JAVA VERSION RUNS SIGNIFICANTLY FASTER THAN THE C++ VERSION!!! WHAT UP!?!?!??!?!?
Here is code that you can test yourself.

JAVA
code:
import java.io.*;

public class Fibonacci
{
// Restrictions: n >= 0
public static int fibonacci(int i)
{
if (i == 1 || i == 0)
return 1;
else
{
return (fibonacci(i - 1) + fibonacci(i - 2));
}

}

public static void main(String [] args) throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter an integer 'n' which will be used to find the nth Finonacci number:");
int index = (new Integer(br.readLine())).intValue();
while (index < 0)
{
System.out.println("Enter a nonnegative integer.");
index = (new Integer(br.readLine())).intValue();
}
System.out.println("The number is " + fibonacci(index));
}
}

C++
code:
#include <iostream.h>
#include <conio.h>


int fibonacci(int);
void main()
{
int index;
cout << "Enter the int which will be used to find the nth Finonacci number:";
cin >> index;
while (index < 0)
{
cout << "Enter a positive integer.";
cin >> index;
}
cout << "The int is " << fibonacci(index);
getch();
}

int fibonacci(int i)
{
if (i == 1 || i == 0)
return 1;
else
{
return (fibonacci(i - 1) + fibonacci(i - 2));
}

}



--------------------
 -

Posts: 1768 | From: Nashville, TN | Registered: Apr 2002  |  IP: Logged
SkyRat
Resident Director of Personal
Member # 10

 - posted      Profile for SkyRat     Send New Private Message       Edit/Delete Post   Reply With Quote 
Java sucks balls for large apps and is the most bloated language to deal with on a large scale. Your code may run rather fast because it is still relatively small, and hence very little has to be compiled at run time. Java is compiled and then executed at runtime, C/C++ is just executed. When you have very little code to compile as does your program, then the efficiency of the compiler comes into play. Java must execute faster than C++, perhaps because it is newer and has been more streamlined, or the java compiler you have is better than the C++ one. Try using GCC (a compiler for C++) and passing a flag of -O3 to it for full optimization and see if this changes the run-time story.

My C++ IDE (Ineractive Development Environment) named Eclipse, like Visual Studio except for linux, is programmed in java and its a total resouce hog because of Java's semi-mostly-compile at run-time properties. 50% of my cpu and a lot of my 384MB of memory is eaten away.

[ March 11, 2003, 09:38 PM: Message edited by: SkyRat[] ]

--------------------
 -  -
quote:
With the stupid limit on sigs my googlism won't fit!!


Posts: 2518 | From: BFH | Registered: Apr 2002  |  IP: Logged
bigO
He Who Knows All
Member # 7

 - posted      Profile for bigO     Send New Private Message       Edit/Delete Post   Reply With Quote 
Yes, for a fibanacci sequence, which is all contained in one class, uses only primitive data types, and makes just one function call, java can be as fast (the fact that it is faster for you probably is an optimization problem)

here is an article that takes it into depth: http://www.jelovic.com/articles/why_java_is_slow.htm

It actually mentions the fib. seq. Cool stuff.

[ March 11, 2003, 11:08 PM: Message edited by: MalliO ]

--------------------
Anybody can be cool, but AWESOME takes practice.

Posts: 4521 | From: VP luv | Registered: Apr 2002  |  IP: Logged
Atomic-Ant
In Real Life Now
Member # 2

 - posted      Profile for Atomic-Ant     Send New Private Message       Edit/Delete Post   Reply With Quote 
That site is AWESOME mike! Thanks a lot. The fact that it mentioned fibonacci specifically is GOLD. That was an interesting problem though... Thanks everyone.

--------------------
 -

Posts: 1768 | From: Nashville, TN | Registered: Apr 2002  |  IP: Logged


 
Post New Topic  New Poll  Post A Reply Close Topic   Feature Topic   Move Topic   Delete Topic next oldest topic   next newest topic
 - Printer-friendly view of this topic
Hop To:

kordix.com

(C)2000-2011 The Boardix Community

Powered by Infopop Corporation
UBB.classicTM 6.5.0