Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

MrPoopypants

Code Optimizing

This topic is 5755 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey guys, I started a C++ class at my High-School and I have been really interested in it. Since one day I hopefully want to be a programmer, I know my best ticket in is writing the best coding possible. Starting from the very basics(This is my first project below) I want to write the fastest, neatest code possible. Any help would be appreciated, thanks a lot!
  
#include <iostream>

using std::cout;

int main()
{

/*	Integers								*/

int iInt1 = 125, iInt2 = 8, iInt3, iInt4, iInt5, iInt6, iInt7;
float fFloat1;
char cChar1;

	fFloat1 = 129.546f;

	cChar1 = ''R'';

	
	iInt3 = iInt1 - iInt2;
	iInt4 = iInt1 + iInt2;
	iInt5 = iInt1 / iInt2;
	iInt6 = iInt1 % iInt2;
	iInt7 = iInt1 * iInt2;

	std::cout << "\nNumber 1 is:    " << iInt1; 
	std::cout << "\nNumber 2 is:    " << iInt2;
	std::cout << "\nNumber 3 is:    " << iInt3;
	std::cout << "\nNumber 4 is:    " << iInt4;
	std::cout << "\nNumber 5 is:    " << iInt5;
	std::cout << "\nNumber 6 is:    " << iInt6;
	std::cout << "\nNumber 7 is:    " << iInt7;


	std::cout << "\n\nFloat is   :    " << fFloat1;
	
	std::cout << "\n\nChar is    :    " << cChar1;

	//This will put "Hit any key to continue" on its own line

	std::cout << "\n\n";

	//Ring bell at end of program

	std::cout << "\a";

	return 0;
}


  
I know this program will run extremely fast anyway, but would it be possible, to say...write all the std::couts on one line to make it run just a tiny bit faster

Share this post


Link to post
Share on other sites
Advertisement
even if all the output lines was on the same line you still have the same number of function calls to make, and if put it on one line is fast the compiler will take care of it.

generally the complier can take care of the optermisation and a problem ALOT of people suffer from it undeeded optermisation at the cost of readablity.

generaly rule of thumb is, make it work, and if you think its running slow, profile it and see where you can improve things, if you can.

this program however it safe to say cant be optermised by you any more without making it close to unreadable, which is more important.

Share this post


Link to post
Share on other sites
Before you get concerned with writing fast code learn the language first and know what you''re doing.

Looking at your code you seem a bit confused about namespaces.

At the top you have

using std::cout;

I don''t think this actually does anything at all to the compilation process. Now if you used

using namespace std;

You would no longer have to write std::cout << "whatever";. Just cout << "whatever";

The reason being that cout is in the std namespace. This means you have to do std::cout to use cout. Now if you put

using namespace std;

at the top this tells the compiler that you have left the std:: off all the stuff that needs it. So you can use cout instead of std::cout

Oh as a side note fast code!=neat code. When you first write code you normally just write it in the most readable form. You save optomization(sp?) till the end. When you do optomize you will find code becomes a lot harder to read and understand

Share this post


Link to post
Share on other sites
Okay, as a new programmer, there is a lesson that you need to learn. DONT MICRO-OPTIMIZE! (micro-optimization is trying to get perfect code with max speed from every line, etc) You shouldn't worry about getting every ounce of speed out of every line of code. Write it efficiently, but don't hack it up to get one clock cycle. Wait until you have finished the entire app, and THEN figure out what is slowing you down. For example, if you have a very small, commonly used function in your app, you might want to rewrite it in asm or just mutilate it for the most speed, but you shouldn't get in the habit of doing it.

Micro-optimization kills most single-man projects, because people (myself included) get so worried about "perfect code", "blazing speed", yada yada yada that they end up having a great framework, or a few great classes, but the project never gets finished.

So - DONT DO IT! - it would be best for you to understand that now before you get in the habit!

(I'm sure many people have replied before me, to the same effect)

Also, that program is perfectly fine. It is a good example of efficient code without over optimization.

[edited by - Neosmyle on September 15, 2002 3:55:56 PM]

Share this post


Link to post
Share on other sites
No, I think that code is close to optimal already. Making the code faster is more about choosing appropriate algorithms than moving individual statements around.

Share this post


Link to post
Share on other sites
quote:
Original post by Monder
using std::cout;

I don''t think this actually does anything at all to the compilation process. Now if you used

using namespace std;

You would no longer have to write std::cout << "whatever";. Just cout << "whatever";

That is only partly correct. The command using std::cout; has the same effect as using namespace std; but only for cout. For example:

//normally
cout << "hello world";

//with whole namespace
using namespace std;
cout << "hello world";

//with std::cout
using std::cout;
cout << "hello world";
cin >> x; //error, need std::cin
std::cin >> y; //good

So to MrPoopypants, you do not need to type std::cout every time you wish to call cout. Because you are using std::cout, you can use the syntax

cout << "\nNumber 1 is: " << iInt1;

Share this post


Link to post
Share on other sites
To do with the ''using std::cout;'' and ''using namespace std;'' stuff: when I was working on my first programs a few years back, my book didnt use any of those, and I dont either. It compiles and runs with just an ''#include <iostream.h>'' as i was tought. And why are people leaving out the .h''s now. Is there a change in standard, or C++?

-J

Share this post


Link to post
Share on other sites
The definition of fast is that it runs in polynomial time as a function of the length of input. Your program does nothing that increases with the amount of input(you have no input), so you have pretty much a constant complexity. So basically, that program runs at the speed of light as far as complexity analysis is concerned.

Theoretically, the compiler could optimize it all away. In other words, just precalculate the output and just copy that into the console on each run.

To answer Jason2Jason, the .h was an early standard. It''s still included for reverse compatability in most compilers, but it''s deprecated.

Share this post


Link to post
Share on other sites
That code is pretty much optimized, but I have to say one thing (that hasn''t been said). Assign a value on initialization


  
float fFloat1 = 129.546f;
char cChar1 = ''R'';

int iInt1 = 125;
int iInt2 = 8;
int iInt3 = iInt1 - iInt2;
int iInt4 = iInt1 + iInt2;
int iInt5 = iInt1 / iInt2;
int iInt6 = iInt1 % iInt2;
int iInt7 = iInt1 * iInt2;

Looks much nicer.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!