• 9
• 13
• 18
• 19
• 27

More efficient?

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

Recommended Posts

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


Share on other sites
Quote:
 All of the code above is preparing for the line:lstrcpy(lpOutput, szFile + i); // if Blstrcpy(lpOutput, i + 1); // if A

Well then there is a scope issuse in B in respect to i.

Share on other sites
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)

Share on other sites
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.

Share on other sites
Quote:
 Original post by geekalertI 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 Blstrcpy(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.

Share on other sites
oops. B was supposed to be 'reversed':

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

sorry for any confusion

Share on other sites
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
{
}

Share on other sites
hmmmm

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

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

Share on other sites
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.

Share on other sites
Quote:
 Original post by geekalertB: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.