473,473 Members | 2,196 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Query regarding optimization of my code

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

TAG=MyString
Eg:
Expand|Select|Wrap|Line Numbers
  1. FRUIT=I like mangoes
  2. CARS=I like red cars
  3. .
  4. .
  5. .
  6. PUSHUPS=I can do a zillion non-stop
  7.  
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:
Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2. char *fill_up_this_string_1;
  3. .
  4. .
  5. .
  6. .
  7. char *fill_up_this_string_100;
  8. int main()
  9. {
  10.    /* Open the file */
  11.  
  12. while( fgets(current_line, MAX_MYSTRING_SIZE + MAX_TAG_SIZE, txtfile))
  13. {
  14.    /* Search for the tag in line */
  15.    if( strcmp("FRUIT", /*the tag found in the line*/) == 0)
  16.    {
  17.        /* malloc memory for fill_up_string_1  */
  18.        strcpy(fill_up_string_1, /* Mystring foung in line */)
  19.    }
  20.    if( strcmp("CARS", /*the tag found in the line*/) == 0)
  21.    {
  22.        /* malloc memory for fill_up_string_2  */
  23.        strcpy(fill_up_string_2, /* Mystring foung in line */)
  24.    }
  25.    if( strcmp("PUSHUPS", /*the tag found in the line*/) == 0)
  26.    {
  27.        /* malloc memory for fill_up_string_3  */
  28.        strcpy(fill_up_string_3, /* Mystring foung in line */)
  29.    }
  30.    .
  31.    .
  32.    .
  33.    .
  34. /* About 200 times */
  35.  
  36.  
  37. }
  38.  
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
Apr 29 '09 #1
3 1760
Banfa
9,065 Recognized Expert Moderator Expert
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.
Apr 29 '09 #2
srbakshi
18 New Member
@Banfa
Thank you Mr.Banfa for your expert comments. :O) I'll try the hash method as well just for fun.
Thanks again,
Sid
:O)
Apr 29 '09 #3
donbock
2,426 Recognized Expert Top Contributor
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_string_N character pointers.
Apr 29 '09 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

13
by: aaron | last post by:
I have a question about (i think) joining. If I have a table in a database that has this info: key - name - favorite 1 - john - 2 2 - judy - 3 3 - joe - 1 the favorite icecream...
6
by: Umar Farooq | last post by:
Hello all, Please bear with the long explanation of my scenario. As I'm relatively new to the query world, I like to write my queries using the visual toos such as the "View" option in SQL...
5
by: AC Slater | last post by:
Whats the simplest way to change a single stored procedures query optimization level? In UDB8 that is. /F
1
by: Sean C. | last post by:
Helpful folks, I have recently migrated our test server, which runs Win NT 4, from V7.2 FP11 to V8.1.3. Just about everything works wondefully, except I am having major problems getting the...
2
by: Eugene | last post by:
I am trying to set query optimization class in a simple SQL UDF like this: CREATE FUNCTION udftest ( in_item_id INT ) SPECIFIC udftest MODIFIES SQL DATA RETURNS TABLE( location_id INT,...
12
by: WantedToBeDBA | last post by:
Hi all, db2 => create table emp(empno int not null primary key, \ db2 (cont.) => sex char(1) not null constraint s_check check \ db2 (cont.) => (sex in ('m','f')) \ db2 (cont.) => not enforced...
4
by: Bernard Dhooghe | last post by:
To retrieve data from a query where multiple rows can be returned, a cursor can be used. Different programming interface exist for cursors: embedded SQL, CLI, SQL PL, SQLJ, JDBC. I we look at...
6
by: Ryan | last post by:
I came across a situation that I've been unable to explain and was hoping somebody had an answer: I had written an update query which was taking about 8 seconds to run and considered it too...
4
by: sanonward001 | last post by:
Hi, This is regarding the query optimization. T_A table is having rows around 570000, and this query's cost comes out to 202. SELECT COUNT(1) FROM T_A WHERE...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.