Jump to content
  • Advertisement
Sign in to follow this  
geekalert

More efficient?

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

If you intended to correct an error in the post then please contact us.

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


Link to post
Share on other sites
Advertisement
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 == '\\')
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.



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 == '\\')
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!