C and C++ Timesaving Tips

Please support our C++ advertiser: Intel Parallel Studio Home
Closed Thread

Join Date: Jun 2005
Posts: 275
Reputation: andor has a spectacular aura about andor has a spectacular aura about andor has a spectacular aura about 
Solved Threads: 29
andor's Avatar
andor andor is offline Offline
Posting Whiz in Training

Re: C and C++ Timesaving Tips

 
0
  #31
Jul 18th, 2006
If you don't care about loop order in for loop then instead this
  1. for( i=0; i<SIZE; i++)
you can use
  1. for( i=SIZE; i--; )
and the loop should be faster
If you want to win, you must not loose (Alan Ford)
Quick reply to this message  
Join Date: Jul 2006
Posts: 2
Reputation: kharesunil is an unknown quantity at this point 
Solved Threads: 0
kharesunil kharesunil is offline Offline
Newbie Poster

Re: C and C++ Timesaving Tips

 
0
  #32
Jul 18th, 2006
Originally Posted by Narue
>i'd just write a new function and get it over with
That's one way to go about it, but since fgets is already there and is presumably written with performance in mind, it's smarter to make use of it:
  1. char *reads ( char *s, size_t limit, FILE *stream )
  2. {
  3. if ( fgets ( s, limit, stream ) == NULL ) {
  4. return NULL;
  5. } else {
  6. char *newline = strchr ( s, '\n' );
  7.  
  8. if ( newline != NULL )
  9. *newline = '\0';
  10.  
  11. return s;
  12. }
  13. }
It's also easier to verify correctness when you use the standard library instead of trying to rewrite it.

I Think this approach will save the time for developer but it will consume more cpu cycles, It's true that fgets enters new line character at the end of string so take the length of string and modify last character to '\0' , I would suggest
  1. char *reads ( char *s, size_t limit, FILE *stream )
  2. {
  3. if ( fgets ( s, limit, stream ) == NULL ) {
  4. return NULL;
  5. } else {
  6. int len = strlen(s);
  7. s[len-1] = '\0';
  8. return s;
  9. }
  10. }

Correect me if i am wrong:lol:

Suneel
Last edited by kharesunil; Jul 18th, 2006 at 2:59 pm.
Quick reply to this message  
Join Date: Apr 2004
Posts: 4,334
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 234
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: C and C++ Timesaving Tips

 
0
  #33
Jul 18th, 2006
Originally Posted by andor
and the loop should be faster
Micro-optimization should be the last item on your list of things to do.
Originally Posted by kharesunil
Correect me if i am wrong:lol:
You need to check for the existance of a newline before you attempt to blow it away. Or just go with what was written -- when waiting for user input, you aren't doing anything really speedy anyways.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Quick reply to this message  
Join Date: Jun 2005
Posts: 275
Reputation: andor has a spectacular aura about andor has a spectacular aura about andor has a spectacular aura about 
Solved Threads: 29
andor's Avatar
andor andor is offline Offline
Posting Whiz in Training

Re: C and C++ Timesaving Tips

 
0
  #34
Jul 19th, 2006
Originally Posted by Dave Sinkula
Micro-optimization should be the last item on your list of things to do.
If working on PC - yes
but if working on 8 - bit TV proccesors - no
If you want to win, you must not loose (Alan Ford)
Quick reply to this message  
Join Date: Apr 2004
Posts: 4,334
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 234
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: C and C++ Timesaving Tips

 
0
  #35
Jul 19th, 2006
Originally Posted by andor
If working on PC - yes
but if working on 8 - bit TV proccesors - no
I've only run into the "need" to do this for parts with less than 2k of code space. Down in that realm, you really ought to consider assembly anyway.

But first, consider that the audience here is 99% PC folks. Not that that needs to mean anything...

But second, remember that no C construct can be generalized as being "faster" or "more efficient". What's good advice in the '60's and '70's may be bad advice today. (I seem to remember "common knowledge" was never to compare to zero.) Likewise, what is good advice in the '80's and '90's may not be solid advice today. (Like this.)

For example, with no optimiztions comparing these two constructs it could be argued that this code generates less efficient output for the loop.
   ;	
   ;	void foo(int SIZE)
   ;	
	push      ebp
	mov       ebp,esp
	push      ebx
	push      esi
	mov       esi,dword ptr [ebp+8]
   ;	
   ;	{
   ;	   int i;
   ;	   for ( i=0;  i<SIZE;  i++ )
   ;	
?live1@16: ; ESI = SIZE
@1:
	xor       ebx,ebx
	cmp       esi,ebx
	jle       short @3
   ;	
   ;	   {
   ;	      printf("%d\n", i);
   ;	
?live1@32: ; EBX = i, ESI = SIZE
@2:
	push      ebx
	push      offset s@
	call      _printf
	add       esp,8
	inc       ebx
	cmp       esi,ebx
	jg        short @2
   ;	
   ;	   }
   ;	}
   ;	
?live1@48: ; 
@3:
@5:
	pop       esi
	pop       ebx
	pop       ebp
	ret 
_foo	endp
_bar	proc	near
?live1@64:
   ;	
   ;	void bar(int SIZE)
   ;	
	push      ebp
	mov       ebp,esp
	push      ebx
   ;	
   ;	{
   ;	   int i;
   ;	   for ( i=SIZE; i--; )
   ;	
@6:
	mov       ebx,dword ptr [ebp+8]
	jmp       short @8
   ;	
   ;	   {
   ;	      printf("%d\n", i);
   ;	
?live1@96: ; EBX = i
@7:
	push      ebx
	push      offset s@+4
	call      _printf
	add       esp,8
@8:
	mov       eax,ebx
	add       ebx,-1
	test      eax,eax
	jne       short @7
   ;	
   ;	   }
   ;	}
   ;	
?live1@112: ; 
@10:
	pop       ebx
	pop       ebp
	ret 
_bar	endp
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Quick reply to this message  
Join Date: Jul 2006
Posts: 2
Reputation: kharesunil is an unknown quantity at this point 
Solved Threads: 0
kharesunil kharesunil is offline Offline
Newbie Poster

Re: C and C++ Timesaving Tips

 
0
  #36
Jul 20th, 2006
Originally Posted by andor
If working on PC - yes
but if working on 8 - bit TV proccesors - no


That's true, but this is good practice to write code in such a manner where minimum optimization required
Quick reply to this message  
Join Date: May 2006
Posts: 5
Reputation: g3 creative is an unknown quantity at this point 
Solved Threads: 0
g3 creative g3 creative is offline Offline
Newbie Poster

Re: C and C++ Timesaving Tips

 
0
  #37
Jul 25th, 2006
Fantastic guys - you learn so much from just browsing.

The Guru
G3 Creative Scotland
Quick reply to this message  
Join Date: Jul 2006
Posts: 4
Reputation: chaosfromthesky is an unknown quantity at this point 
Solved Threads: 0
chaosfromthesky's Avatar
chaosfromthesky chaosfromthesky is offline Offline
Newbie Poster

Re: C and C++ Timesaving Tips

 
0
  #38
Jul 29th, 2006
This is one of the way more obvious tips but people always seem to not end up doing this. When creating a code (any code be it c++ - HTML) it is extremely time consuming to find errors if you do not keep it well organized.
That means that first and foremost, your brackets are key. Don't add in excess because its just another thing to keep track of and if ur coding I'm sure u want no more of that.
Second, double space almost everwhere becuase it makes the code 10x easier to read.
Third. If you are concentrating on a certain area of code a great thing to do is add // to the obsolete parts.
*side note: if trying to build a code that forms thousands of random numbers per minute and outputs them into notepad (or other) with the intent to crash someone else's computer, make sure it con't crash your own first lol.
Quick reply to this message  
Join Date: Apr 2004
Posts: 4,334
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 234
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: C and C++ Timesaving Tips

 
0
  #39
Jul 29th, 2006
Originally Posted by chaosfromthesky
t is extremely time consuming to find errors if you do not keep it well organized.

That means that first and foremost, your brackets are key. Don't add in excess because its just another thing to keep track of and if ur coding I'm sure u want no more of that.
If you mean skip full bracing, this would be contrary to common wisdom. Full bracing is always preferred.

Originally Posted by chaosfromthesky
Third. If you are concentrating on a certain area of code a great thing to do is add // to the obsolete parts.
Again, unless you can add more detail, this is contrary to common wisdom. Using the preprocessor would be preferred.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Quick reply to this message  
Join Date: Jul 2006
Posts: 4
Reputation: chaosfromthesky is an unknown quantity at this point 
Solved Threads: 0
chaosfromthesky's Avatar
chaosfromthesky chaosfromthesky is offline Offline
Newbie Poster

Re: C and C++ Timesaving Tips

 
0
  #40
Jul 29th, 2006
nah, sorry that did seem a bit off. What I meant was that you should try to only use the correct amount of brackets instead of adding a whole bunch of brackets at the bigging, a whole bunch at the end, and then just trying to fill in. I know it sounds stupid but I know about 2 or 4 people who add brackets before they even start their actual code. I understand that a lot of brackets are neccesary for a functioning code.
Quick reply to this message  
Closed Thread

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC