Query regarding optimization of my code

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • srbakshi
    New Member
    • Aug 2008
    • 18

    Query regarding optimization of my code

    Hi all!
    I have a txt file that has some strings in the following format:

    TAG=MyString
    Eg:
    Code:
    FRUIT=I like mangoes
    CARS=I like red cars
    .
    .
    .
    PUSHUPS=I can do a zillion non-stop
    Now what I'm doing is search the TAGs in the file and storing the corresponding string associated with the TAG in a global string (1 for every tag) Something like this:
    Code:
    #include<stdio.h>
    char *fill_up_this_string_1;
    .
    .
    .
    .
    char *fill_up_this_string_100;
    int main()
    {
       /* Open the file */
    
    while( fgets(current_line, MAX_MYSTRING_SIZE + MAX_TAG_SIZE, txtfile))
    {
       /* Search for the tag in line */
       if( strcmp("FRUIT", /*the tag found in the line*/) == 0)
       {
           /* malloc memory for fill_up_string_1  */
           strcpy(fill_up_string_1, /* Mystring foung in line */)
       }
       if( strcmp("CARS", /*the tag found in the line*/) == 0)
       {
           /* malloc memory for fill_up_string_2  */
           strcpy(fill_up_string_2, /* Mystring foung in line */)
       }
       if( strcmp("PUSHUPS", /*the tag found in the line*/) == 0)
       {
           /* malloc memory for fill_up_string_3  */
           strcpy(fill_up_string_3, /* Mystring foung in line */)
       }
       .
       .
       .
       .
    /* About 200 times */
      
    
    }
    So I have to have like 200 if-else statements to accomplish this task. Is there a shorter way to do this? Also, any tag found at the end of the file will lead to execution of all the strcmp statements before being matched successfully. I dont think this is a good approach. How would you pros do it?
    Thanks in advance,
    Sid
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    Use an array of strings or of const char * to hold the tag names, then use a for loop to iterate through this array and use a generalised version of one of you if statements inside the for loop.

    To avoid comparing against every tag to find the last one you could read the tag from the file, create a hash of it, store your list of tags in a hash table and use the hash to loop up the correct entry in the hash table.

    However unless you have more than a few 100 tags this is unlikely to produce much of noticeable performance improvement and you may as well stick with the simple array and for loop lookup.

    Comment

    • srbakshi
      New Member
      • Aug 2008
      • 18

      #3
      Originally posted by Banfa
      Use an array of strings or of const char * to hold the tag names, then use a for loop to iterate through this array and use a generalised version of one of you if statements inside the for loop.

      To avoid comparing against every tag to find the last one you could read the tag from the file, create a hash of it, store your list of tags in a hash table and use the hash to loop up the correct entry in the hash table.

      However unless you have more than a few 100 tags this is unlikely to produce much of noticeable performance improvement and you may as well stick with the simple array and for loop lookup.
      Thank you Mr.Banfa for your expert comments. :O) I'll try the hash method as well just for fun.
      Thanks again,
      Sid
      :O)

      Comment

      • donbock
        Recognized Expert Top Contributor
        • Mar 2008
        • 2427

        #4
        I think I would construct a symbol table from the tag file; where each entry in the table has two fields: tag name and tag value. Organize the table so it is keyed by tag name -- a binary tree is a reasonable choice.

        Step 1: scan the tag file and construct the symbol table.

        Step 2: do whatever else your program has to do; performing a symbol table lookup whenever you need a tag value. Decide how you want to handle lookup failure (desired tag not defined in the file).

        Advantages of this method:
        1. Order of tag definitions is irrelevant.
        2. New tags can be added without making any changes to the scanning code.
        3. Tag usage is accomplished through the more intuitive tag name rather than the harder to keep track of fill_up_this_st ring_N character pointers.

        Comment

        Working...