Sign in to follow this  
geekalert

More efficient?

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[i] == '\\')
        break;
}
All of the code above is preparing for the line:
lstrcpy(lpOutput, szFile + i);    // if B
lstrcpy(lpOutput, i + 1);         // if A

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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 this post


Link to post
Share on other sites
Guest Anonymous Poster
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 this post


Link to post
Share on other sites
Guest Anonymous Poster
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[i] == '\\')
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[i] == '\\')
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 this post


Link to post
Share on other sites
Guest Anonymous Poster
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;
}

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
hmmmm

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

should read

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

Share this post


Link to post
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 this post


Link to post
Share on other sites
Quote:
Original post by geekalert

B:

for(int i = 0; i < lstrlen(szFile); i++)
{
if(szFile[i] == '\\')
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[i] == '\\')
break;
}


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

Share this post


Link to post
Share on other sites
You almost ALWAYS want to write clean, easy to read code whenever possible even if it costs a few extra CPU cycles. Writting messy code can bite you in the butt, costing hours of debugging hassle for you.

And when in doubt follow these simple instructions...

1) Write it so it works
2) Write it so it works faster

Share this post


Link to post
Share on other sites
Be sure that you understand how C++ bascially handles strings (char[]'s with null terminators) before you start doing anything complex with them.
Otherwise, follow Zahlman's advice and stick with std::string.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this