More efficient?

Started by
10 comments, last by deadimp 17 years, 11 months ago
I just have a quick question: out of A and B, would you say that A is more efficient? And safer? Thanks. A:

char* i = szFile + lstrlen(szFile);
while((i--) != szFile && (*i) != '\\');
B:

for(int i = 0; i < lstrlen(szFile); i++)
{
    if(szFile == '\\')
        break;
}
All of the code above is preparing for the line:

lstrcpy(lpOutput, szFile + i);    // if B
lstrcpy(lpOutput, i + 1);         // if A
A JPEG is worth a thousand and twenty four DWORD's. ;)
Note: Due to vacationing my website will not be updated till late-August. Of course I still have internet, but who wants to program during a vacation?
Advertisement
Quote:All of the code above is preparing for the line:

lstrcpy(lpOutput, szFile + i); // if B

lstrcpy(lpOutput, i + 1); // if A

Well then there is a scope issuse in B in respect to i.
Can't you use one of these funcs which are standard
char *strchr(const char *string, int c)
char *strrchr(const char *string, int c)
char *strstr(const char *s1, const char *s2)
char *strpbrk(const char *s1, const char *s2)
I think A and B don't even do the same!
A searches for the last \ in the string while the for loop searches for the first occurence.
Quote:Original post by geekalert
I just have a quick question: out of A and B, would you say that A is more efficient? And safer? Thanks.

A:

char* i = szFile + lstrlen(szFile);
while((i--) != szFile && (*i) != '\\');


B:

for(int i = 0; i < lstrlen(szFile); i++)
{
if(szFile == '\\')
break;
}


All of the code above is preparing for the line:

lstrcpy(lpOutput, szFile + i); // if B
lstrcpy(lpOutput, i + 1); // if A



int i;
for(i = 0; i < lstrlen(szFile); i++)
{
if(szFile == '\\')
break;
}
lstrcpy(lpOutput, szFile + i); // if B


This should work though (don't know about the logic, but the syntax is right). Don't use A, it's quite hard to see what it does.



oops. B was supposed to be 'reversed':

for(int i = lstrlen(szFile); i > -1; i--)

sorry for any confusion
A JPEG is worth a thousand and twenty four DWORD's. ;)
Note: Due to vacationing my website will not be updated till late-August. Of course I still have internet, but who wants to program during a vacation?
extern char* szFile;
char* start;
extern char* lpOuput;
start = *strrchr(szFile, '\\');//find last occurance in the string
if(start)//check its valid
{
strcpy(lpOutput,start);
}
else
{
//error character not found in string;
}
hmmmm

start = *strrchr(szFile, '\\');//find last occurance in the string

should read

start = strrchr(szFile, '\\');//find last occurance in the string
If you don't have the knowledge to test for yourself which is faster, then you *definitely* shouldn't be worrying about it. As for safety, they're both *horrible*, and anyway, the C standard library provides the exact same thing. In a way that is pretty much guaranteed to be at least as fast if not faster, because the compiler implementation can replace it with pre-prepared assembly and doesn't have to look at your code and figure out "oh, he just wants a strrchr()".

But even then, any kind of C string manipulation is horrible for safety. If you at all have the option of using C++, you can write something like:

std::string output(filename, 0, filename.rfind('\\') + 1);// +1 is if you want to keep the backslash.


and it will *still* probably be about as fast as whatever you were going to write "the hard way".

But all of that efficiency concern *completely and utterly misguided* anyway, because it will take orders of magnitude more time for the OS to open the file than any of this string processing.
Quote:Original post by geekalert

B:
for(int i = 0; i < lstrlen(szFile); i++){    if(szFile == '\\')        break;}




just a quick note: in a for loop (or any loop, really), you pretty much always want to do something like this:

int len = lstrlen(szFile);for(int i = 0; i < len; i++){    if(szFile == '\\')        break;}


in C++, the strlen() function has O(N) complexity, which is not something you want to do every iteration of a loop.

This topic is closed to new replies.

Advertisement