| | |
C and C++ Timesaving Tips
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
If you don't care about loop order in for loop then instead this
you can use
and the loop should be faster
C++ Syntax (Toggle Plain Text)
for( i=0; i<SIZE; i++)
C++ Syntax (Toggle Plain Text)
for( i=SIZE; i--; )
If you want to win, you must not loose (Alan Ford)
•
•
Join Date: Jul 2006
Posts: 2
Reputation:
Solved Threads: 0
•
•
•
•
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:
It's also easier to verify correctness when you use the standard library instead of trying to rewrite it.C++ Syntax (Toggle Plain Text)
char *reads ( char *s, size_t limit, FILE *stream ) { if ( fgets ( s, limit, stream ) == NULL ) { return NULL; } else { char *newline = strchr ( s, '\n' ); if ( newline != NULL ) *newline = '\0'; return s; } }
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
C++ Syntax (Toggle Plain Text)
char *reads ( char *s, size_t limit, FILE *stream ) { if ( fgets ( s, limit, stream ) == NULL ) { return NULL; } else { int len = strlen(s); s[len-1] = '\0'; return s; } }
Correect me if i am wrong:lol:
Suneel
Last edited by kharesunil; Jul 18th, 2006 at 2:59 pm.
•
•
•
•
Originally Posted by andor
and the loop should be faster
•
•
•
•
Originally Posted by kharesunil
Correect me if i am wrong:lol:
"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
•
•
•
•
Originally Posted by andor
If working on PC - yes
but if working on 8 - bit TV proccesors - no
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
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.
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.
•
•
•
•
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.
•
•
•
•
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.
"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
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.
![]() |
Similar Threads
- Timesaving Tips (C++)
- Response to time saving tips sticky (C++)
Other Threads in the C++ Forum
- Previous Thread: not sure why program crashes
- Next Thread: helpppppp me with this program
| Thread Tools | Search this Thread |
api array based beginner binary c++ c/c++ calculator char char* class classes code compile compiler console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news node numbertoword output parameter pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets





