How can i optimize the follow function?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • pilafi
    New Member
    • Nov 2007
    • 15

    How can i optimize the follow function?

    [CODE=c]char start=0;
    char isfloat=0;
    char tmp_num[10];
    int i=0,j=0;
    char *end;

    memset(tmp_num, 0,10);

    end=(*str)+strl en(*str);

    while(((*str)++ )<=end)
    {
    if( isdigit(**str) )

    {
    start=1;
    tmp_num[j] = **str;
    j=j+1;
    }
    if( ispunct(**str) && start==1)

    {
    isfloat=1;
    tmp_num[j] = **str;
    j=j+1;
    }

    if( (**str==' ') || (**str==0) )
    {
    if(isfloat==1 && start==1)
    {
    *d=atof(tmp_num );
    return 0;
    }
    if(*(*str+1)==0 )
    {
    return 1;
    }
    start=0;
    isfloat=0;
    j=0;
    memset(tmp_num, 0,10);
    }

    }

    return 1;
    }[/CODE]


    I have large data sets and this functios creates problems because of the isdigit and ispunct function. Do you have any idea to replace them??
    Thnx guys!!
    Last edited by Ganon11; Nov 22 '07, 04:05 PM. Reason: Please use the [CODE] tags provided.
  • Cucumber
    New Member
    • Sep 2007
    • 90

    #2
    Well, you can save a call to

    memset(tmp_num, 0,10);

    using

    tmp_num[0]=0;
    and doing
    tmp_num[j++] = **str;
    tmp_num[j]=0;

    I dont know whether that would help.

    Anyway, try using some optimization flag in your compiler, like -O2 or -O3 and see if your program speed improves.

    Comment

    • pilafi
      New Member
      • Nov 2007
      • 15

      #3
      I tried to use the optimization flags, but the speed doesnt change. I forgot to give you a detail that this function is a function for getting the next double you find in the file. It works normally, but i have some files with a lot of doubles, really lot and my program crasses in these files. Please help guys!!Thnx!!

      Comment

      Working...