Optimization while mixing c style strings with c++ strings -
Optimization while mixing c style strings with c++ strings -
i trying solve programming problem website , got time limit exceed. i'm trying alter parts of code utilize c++ string c style strings.
here part code wanted 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, of variables above c++ strings, have changed uppercase ones c-style, assignments no longer valid...
what fastest way initialize lower-case ones?
x1 = x1; x1 += x2; x1 += x3;
or
char buffer[20]; //would utilize x1 instead of buffer if reply sec question //is convert it(x1) c-style strcpy(buffer, x1); strcat(buffer, x2); strcat(buffer, x3); x1 = buffer;
the utilize lowercase ones in comparison:
if(current == x1 || current == x2 || current == x3 || current == x4 || current == x5 || current == x6)
where 'current' c++ string (and 1 won't alter because i'm updating value through elements within container)
this if going executed many times, want know if improve allow x1 ... x6 c++ strings (i suppose if compare c++ string c-style string phone call constructor c++ string , pass c-style argument before comparison).
edit:
reformulating: want know in sec is:
when create comparing this:
string st = "something"; char st2[20] = "other thing"; if(st == st2)
is going phone call constructor string(st2) , compare constructed string 1 @ left? lets comparing 500000x, faster if st2 c++ string?
edit2: finish code here
if want speed, don't create string in order comparison; especially, don't create 6 strings since might need 1 or 2 of them. it's not relevant whether c strings or c++ strings.
do know how long x1
, x2
, x3
are? it's easy plenty find out, if not. assuming do, want know 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 more readable, of course, , mine might have typos.
i suspect this, too, unnecessary; need reexamine design. why using concatenated strings instead of, example, tuples of little integers?
c++ c string optimization c-strings
Comments
Post a Comment