Computer Science Canada Turing VS C++ (Recursive Counting) |
Author: | Flikerator [ 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++
Turing
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 ^_^ |
Author: | jamonathin [ Tue Jun 06, 2006 12:20 pm ] | ||
Post subject: | |||
C++ wins again. One thing you may want to use is:
Im not shure the difference between using it or not, but i would rathe write that once then std:: a million times. |
Author: | Flikerator [ Tue Jun 06, 2006 1:22 pm ] |
Post subject: | |
Nah I prefer to use "std::" instead of including namespace. Thanks though =P I think C++ will usually win =P |
Author: | wtd [ Tue Jun 06, 2006 1:43 pm ] |
Post 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. |
Author: | Andy [ Tue Jun 06, 2006 4:50 pm ] |
Post 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. |
Author: | Mazer [ Tue Jun 06, 2006 5:35 pm ] |
Post subject: | |
That's four. |
Author: | [Gandalf] [ Tue Jun 06, 2006 6:33 pm ] |
Post 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. |
Author: | md [ Tue Jun 06, 2006 7:51 pm ] |
Post 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. |
Author: | wtd [ Tue Jun 06, 2006 8:25 pm ] |
Post 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. |
Author: | Andy [ Tue Jun 06, 2006 11:04 pm ] |
Post 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 |
Author: | wtd [ Tue Jun 06, 2006 11:23 pm ] |
Post 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
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... |
Author: | Flikerator [ Thu Jun 08, 2006 11:59 am ] |
Post 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). |
Author: | wtd [ Thu Jun 08, 2006 12:05 pm ] |
Post 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?" |
Author: | Flikerator [ Thu Jun 08, 2006 12:09 pm ] |
Post 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 |
Author: | wtd [ Thu Jun 08, 2006 12:25 pm ] |
Post subject: | |
Zero indicates success. Everything else is failure... of some sort. |
Author: | Flikerator [ Fri Jun 09, 2006 11:57 am ] | ||
Post subject: | |||
Don't really know what to do to learn some more C++, having trouble finding anything about Voids (Except programs that already have them, in which case they don't explain what they are, or how they work). I guess I could mess around and figure them out, but I like tutorials (More then one, and then experimenting, so I don't get the wrong idea). Since I was bored, I made an extremely simple recursive factoring program. Im terrible at recursive programs (I can formulate them in my head, but writing the code I have no were to start.), practice breeds improvement right?
If you have any alternative ways to do this recursivly, let me know =P |
Author: | wtd [ Fri Jun 09, 2006 12:10 pm ] | ||||||||||||
Post subject: | |||||||||||||
Let me suggest something here, first. What you have is exactly identical to having an "else" clause. So just use the else clause.
For something this simple, we might also rewrite this as:
Secondly, there's a problem with using floating point numbers in this case. Let's say we write:
Now, since this isn't 1, we next execute:
And 0.5 isn't 1, so next...
Will this ever terminate? |
Author: | Flikerator [ Fri Jun 09, 2006 12:26 pm ] |
Post subject: | |
At first I thought about putting an else, but realised it wasn't needed, because its just extra code for no reason. Unless im missing something. That is really neat. As I understand it; does x == 1? then return 1 Then the ":" would be like an else right? Thats really neat. As for that error, it was meant only to be ints. Why I put Double I have no idea. Ive never heard of Factoring real numbers My Turing version has ints so I guess I goofed =P Thanks wtd for all the help ^_^ |
Author: | Null [ Sat Jun 10, 2006 10:43 am ] |
Post subject: | |
Just a note. This is not rule by any means, but functions in C++ typically begin with a lowercase letter. |
Author: | wtd [ Sat Jun 10, 2006 11:26 am ] | ||||
Post subject: | |||||
Additionally, what is being done is not "factoring", but generating a factorial. The function name should indicate this. A recursive factorial function with O(1) space properties:
Or perhaps:
|
Author: | Andy [ Sat Jun 10, 2006 9:18 pm ] |
Post subject: | |
Null wrote: Just a note. This is not rule by any means, but functions in C++ typically begin with a lowercase letter.
Just a note. This is not a rule by any means, but people generally read a few pages ahead before posting |