Optimize this function

Started by
13 comments, last by Bigshot 21 years, 6 months ago

  
int WordToInteger(Word w)
{
    // convert w into an integer based on the values of letters

    int intnumber = 0;

    for (int i = 0; i < w.numletters; ++i)
    {
        intnumber += Math.pow(10, w.numletters-i-1) * elementAt(w.letters[i].id);
    }

    return intnumber;
}
  
This function is used in solving cryptarithmetic problems to convert a word into an integer, so in most cases it''s called at least several thousand times. Needs to be a lot faster. It''s in java..converting to C would probably help, lol. The function elementAt runs constant and is basically just one line, but to keep abstraction i''m using a function call. I guess I''m looking for a way to speed up the 10^i call which is used to create a number one digit at a time. i.e. (10^0 * 5) + (10^1 * 2) = 25 I thought about doing bit-shifting instead of calling the power function, but then realized it only works in base 2. Any suggestions?
Alexbigshot@austin.rr.comFoolish man give wife grand piano. Wise man give wife upright organ.
Advertisement
Do it in reverse and keep a digit factor:

  int WordToInteger(Word w){    // convert w into an integer based on the values of letters    int intnumber = 0;    int digitfactor = 1;    for (int i = w.numletters-1; i >= 0; i--)    {        intnumber += digitfactor * elementAt(w.letters[i].id);        digitfactor *= 10;    }    return intnumber;}  

No more pow(...) call. You probably want to make sure that the input passed in is valid too.

Cheers, dorix
Thanks! Simple and effective. I just tried it... in a program that called that function over 150,000 times it reduced the running time from 1600 milliseconds to 300
Alexbigshot@austin.rr.comFoolish man give wife grand piano. Wise man give wife upright organ.
And pass by const-reference rather than value.
I thought everything was passed by reference in Java.
Yeah all non-primitive types are like pointers or references.. you don''t even have a choice. When you create a new instance of anything you have to call new.

Well that definitely helped... now i need to optimize other things and try to reduce the number of times i have to call that function.
Alexbigshot@austin.rr.comFoolish man give wife grand piano. Wise man give wife upright organ.
Nothing is pass by reference in Java. Java has no references. Everything is pass by value; it just happens that object variables are pointers to objects on the garbage collected heap, so are passed by pointer value.
char a[99999],*p=a;int main(int c,char**V){char*v=c>0?1[V]:(char*)V;if(c>=0)for(;*v&&93!=*v;){62==*v&&++p||60==*v&&--p||43==*v&&++*p||45==*v&&--*p||44==*v&&(*p=getchar())||46==*v&&putchar(*p)||91==*v&&(*p&&main(0,(char**)(--v+2))||(v=(char*)main(-1,(char**)++v)-1));++v;}else for(c=1;c;c+=(91==*v)-(93==*v),++v);return(int)v;}  /*** drpizza@battleaxe.net ***/

  int WordToInteger(Word w){  int intnumber = 0;  for (int i = 0; i < w.numletters; ++i)  {    intnumber = intnumber * 10 + elementAt (w.letters[i].id);  }  return intnumber;}  


http 500 strike 1..
Stoffel: Your version will not give the same result, you push all digits left then add the new one at the end(right).
Bigshot and dorix add new digits left of all the others.
Ah, you are correct. When I''ve worked with hashing strings before, the first letter is the most significant so my method is the one I use. This will give the same output:

  int WordToInteger(Word w){  int intnumber = 0;  for (int i = w.numletters-1; i >= 0; i--)  {    intnumber = 10 * intnumber + elementAt(w.letters[i].id);  }return intnumber;}  

This topic is closed to new replies.

Advertisement