Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Turing VS C++ (Recursive Counting)
Index -> Programming, C++ -> C++ Submissions
Goto page 1, 2  Next
View previous topic Printable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Flikerator




PostPosted: Tue Jun 06, 2006 11:59 am   Post subject: Turing VS C++ (Recursive Counting)

Turing VS C++ in speed has always been clear, ill make it a little clearer;

C++
code:
#include <iostream>

int Count (int n);

int main()
{
   std::cout << Count (1) << std::endl;
   std::cin.ignore();
}

int Count (int n)
{
    std::cout << n << std::endl;
    return Count (n+1);
}


Turing
code:

procedure Count (n : int)
    put n
    Count (n + 1)
end Count

Count (1)



I just started learning C++ again. I scrapped it in the light of competing, but im back now that I have no competitions until next year. Feel free to criticize my code ^_^
Sponsor
Sponsor
Sponsor
sponsor
jamonathin




PostPosted: Tue Jun 06, 2006 12:20 pm   Post subject: (No subject)

C++ wins again. One thing you may want to use is:
c++:

using namespace std;

Im not shure the difference between using it or not, but i would rathe write that once then std:: a million times.
Flikerator




PostPosted: Tue Jun 06, 2006 1:22 pm   Post subject: (No subject)

Nah I prefer to use "std::" instead of including namespace. Thanks though =P

I think C++ will usually win =P
wtd




PostPosted: Tue Jun 06, 2006 1:43 pm   Post subject: (No subject)

There are lots of optimized compiled languages. You should also be concerned with the ease with which you can write programs in a given language. Here C++ is not necessarily going to be a winner.
Andy




PostPosted: Tue Jun 06, 2006 4:50 pm   Post subject: (No subject)

a couple of things,

functions usually dont start with capitals
your main is missing a return statement
your Count function should not be int, make it void
the std::cin.ignore() line will never be reached since Count recurses infinitely.
Mazer




PostPosted: Tue Jun 06, 2006 5:35 pm   Post subject: (No subject)

That's four.
[Gandalf]




PostPosted: Tue Jun 06, 2006 6:33 pm   Post subject: (No subject)

Some things to keep in mind:
-Turing opens a graphical run window even to output text, C++ uses the console.
-When I did a similar comparison between Java, C++ and C quite a while ago, I noticed that Java finished faster than C++. Does this mean Java is usually faster than C++? I doubt it.
-Note that you will get significantly different results on a test that uses std::cout with std::endl and one that doesn't have the std::endl. Same with a program that uses "\n" instead of std::endl or a program that uses printf() instead of std::cout. You should take all these factors into consideration when doing these kinds of tests.
md




PostPosted: Tue Jun 06, 2006 7:51 pm   Post subject: (No subject)

The difference in times in such a simple program has very little to do with the language used and lots more to do with random OS timing and library calls.

Oh, and Andy nowhere does it say functions can't start with capitals. Just because it's not a convention you use doesn't mean other people can't do it like that.
Sponsor
Sponsor
Sponsor
sponsor
wtd




PostPosted: Tue Jun 06, 2006 8:25 pm   Post subject: (No subject)

[Gandalf] wrote:
-When I did a similar comparison between Java, C++ and C quite a while ago, I noticed that Java finished faster than C++. Does this mean Java is usually faster than C++? I doubt it.


Though I do not have enough information to comment on "usually," dramatic concerns as to the execution speed of Java programs are largely outdated. Further, Java programs can run faster than C++ programs. Java code, after compilation contains significantly more information as to the structure of the original program than does compiled C++ code. Information is the fuel for optimizers.
Andy




PostPosted: Tue Jun 06, 2006 11:04 pm   Post subject: (No subject)

i just thought that if most of the world is conforming to a specific style, you might as well do it just to not confuse others Razz
wtd




PostPosted: Tue Jun 06, 2006 11:23 pm   Post subject: (No subject)

Andy wrote:
i just thought that if most of the world is conforming to a specific style, you might as well do it just to not confuse others Razz


There are good stylistic reasons to avoid capitalized functions, but conventions aren't one of them, where C++ is concerned. Look at Windows API functions... Smile
Flikerator




PostPosted: Thu Jun 08, 2006 11:59 am   Post subject: (No subject)

Functions Can and Shall (in my programs) start with a Capital letter. If you don't like to do that, thats fine I will continue to do so.

std::cin.ignore(); was only in there when I was testing it, and I decided to keep it in because...Okay I won't lie I just didn't think about taking it out.

I havn't learned voids yet, I just started.

Exactly why does main have a return? Im not trying to be arrogant, but I havn't seen any reason to include it.

You can convert turing to a text consol type thing [View.Set ("text")] and although its a lot faster, still dwarfed by speed.

I actually like writing in C++ better then Turing because of all the wierd...well not wierd but you know what I mean...syntax. Searching throughout the entire code for a ";" that you missed, or you put ":" instead helps develop different skills that you probably wouldn't get from a language like Turing (I hear python is similar, in that it has no wierd curly brackets and stuff).
wtd




PostPosted: Thu Jun 08, 2006 12:05 pm   Post subject: (No subject)

Flikerator wrote:
Exactly why does main have a return? Im not trying to be arrogant, but I havn't seen any reason to include it.


The question you wish to ask is:

"How does your program indicate success (or failure) to the operating system?"
Flikerator




PostPosted: Thu Jun 08, 2006 12:09 pm   Post subject: (No subject)

wtd wrote:
Flikerator wrote:
Exactly why does main have a return? Im not trying to be arrogant, but I havn't seen any reason to include it.


The question you wish to ask is:

"How does your program indicate success (or failure) to the operating system?"


Good point. *Is assuming its return 0;". Ill look into it, and include it from no on. Thanks =P
wtd




PostPosted: Thu Jun 08, 2006 12:25 pm   Post subject: (No subject)

Zero indicates success. Everything else is failure... of some sort.
Display posts from previous:   
   Index -> Programming, C++ -> C++ Submissions
View previous topic Tell A FriendPrintable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 2  [ 21 Posts ]
Goto page 1, 2  Next
Jump to:   


Style:  
Search: