nightshade
nightshade

Reputation: 638

Optimization while mixing c style strings with c++ strings

I was trying to solve a programming problem from a website and got time limit exceed. Now I'm trying to change some parts of my code where I use C++ string to C style strings.

Here is a part from my code that I wanted some advice:

    x1 = X1 + X2 + X3;
    x2 = X1 + X3 + X2;
    x3 = X2 + X1 + X3;
    x4 = X2 + X3 + X1;
    x5 = X3 + X2 + X1;
    x6 = X3 + X1 + X2;

Before, all of those variables above were C++ strings, now I have changed the uppercase ones to C-style, so those assignments are no longer valid...

What would be the fastest way to initialize the lower-case ones?

x1 = X1; 
x1 += X2; 
x1 += X3;

or

char buffer[20]; //would use x1 instead of a buffer if the answer to the second question 
                 //is to convert it(x1) to C-style
strcpy(buffer, X1); 
strcat(buffer, X2); 
strcat(buffer, X3); 
x1 = buffer;

the only use for the lowercase ones is in this comparison:

if(current == x1 || current == x2 || current == x3 || current == x4 || current == x5 || current == x6)

where 'current' is C++ string (And this one I won't change because I'm updating its value through elements inside a container)

this IF is going to be executed MANY times, so I want to know if it is better to let x1 ... x6 as C++ strings (I suppose if I compare C++ string with a C-style string it will call a constructor from C++ string and pass the C-style as argument before the comparison).

EDIT:

Reformulating: I what I want to know in the second is:

When I make a comparison like this:

string st = "something";
char st2[20] = "other thing";
if(st == st2) 

Is it going to call the constructor string(st2) and the compare constructed string to the one at the left? Lets say I do this comparison 500000x, would it be faster if st2 were already a C++ string?

EDIT2: The complete code is here

Upvotes: 0

Views: 129

Answers (1)

rici
rici

Reputation: 241701

If you want speed, don't create a string just in order to do a comparison; especially, don't create six strings since you might sometimes only need one or two of them. It's not relevant whether they are C strings or C++ strings.

Do you know how long X1, X2 and X3 are? It's easy enough to find out, if not. Assuming you do, what you want to know is something like:

if (   current.compare(0, lenX1, X1) == 0 &&
       (   current.compare(lenX1, lenX2, X2) == 0
           && current.compare(lenX1+lenX2, lenX3, X3) == 0 
        || current.compare(lenX1, lenX3, X3) == 0
           && current.compare(lenX1+lenX3, lenX2, X2) == 0)
    || current.compare(0, lenX2, X2) == 0 &&
       (   current.compare(lenX2, lenX1, X1) == 0
           && current.compare(lenX2+lenX1, lenX3, X3) == 0
        || current.compare(lenX2, lenX3, X3) == 0
           && current.compare(lenX2+lenX3, lenX1, X1) == 0)
    || current.compare(0, lenX3, X3) == 0 &&
       (   current.compare(lenX3, lenX1, X1) == 0
           && current.compare(lenX3+lenX1, lenX2, X2) == 0
        || current.compare(lenX3, lenX2, X2) == 0
           && current.compare(lenX3+lenX2, lenX1, X1) == 0))

Your version is more readable, of course, and mine might have typos.

I suspect that this, too, is unnecessary; you need to reexamine your design. Why are you using concatenated strings instead of, for example, tuples of small integers?

Upvotes: 2

Related Questions