Welcome to Linux Forums!

With a comprehensive Linux Forum, information on various types of Linux software and many Linux Reviews articles, we have all the knowledge you need a click away, or accessible via our knowledgeable members.

Linux Forum ArticlesLinux ForumsLinux Forum DownloadsLinux Hosts
Home|Register|FAQ|Member List|Calendar|Unanswered Posts|Forum Rules|Today's Posts|Advanced Search|
SEARCH FOR IN
Go Back   Linux Forums > GNU Linux Zone > Linux Programming & Scripting
Reload this Page Slow C program performance
Linux Forums
Linux Forums
Welcome To The Linux Forums!
Welcome to Linux Forums. We pride ourselves in being one of the largest Linux communities on the web, we encourage you to REGISTER on our forums and participate in the community. There are over 150,000 members ready to answer your questions. JOINING US today will allow you to make new posts, get support, send messages to other members and submit downloads to our downloads directory and many other great features!

Linux Programming & Scripting C, Perl, PHP, Bash Scripts, anything programming or script related post in here!

Reply
 
Thread Tools Display Modes
Old 12-27-2007   #1 (permalink)
StarGhost
Just Joined!
 
Join Date: Nov 2006
Posts: 13
Slow C program performance

Hello mate,

I'm learning C in Linux and just encountered some issue which I could not solve myself. Here is part my code
Code:
char *wordlist[500000];
int main()
{
  FILE *file;
  long i=0;
  char *tmp = malloc(100);  
  char *convert = malloc(20);
  
  long count=0;
  wordlist[0]=calloc(500000,30*sizeof(char));  
  for (i=1;i<500000;i++)
      wordlist[i]=wordlist[i-1]+30;
  strcpy(name,"/grid/grid21dec07/ProcessOutput/out");
  for (i=0;i<=1000;i++)
  {
    strcpy(tmp,name);
    sprintf(convert,"%d",i);
    strcat(tmp,convert);
    strcat(tmp,"/SuccessList.txt");
    file = fopen(tmp,"r");
    if (file == (FILE *)0)
    {
      printf("File %s cannot be openned. Program terminated.",tmp);
      return -1;
    }
    while (fgets(wordlist[count],100,file)!=(char *)NULL)
    {
      count++;
    }
    fclose(file);
  }
  free(wordlist[0]);
  free(line);
  free(tmp);
  free(convert);
  free(name);
}
Basically, my program just read a lot of files (around 50000), each of which contains several lines. The program will read these lines into the string array wordlist[].

I'm currently using FC7, and gcc as compiler. The problem is when I started running the program, it was so slow that I had to stop it in the middle, review the code to see if there is any infinite loop, then run again. In the second running, I realized that all the parts that it has ran through in the previous time was done in no time, and then slow again.

After several times, the program now run in alomost no time. But if I wait for few hours, or restart the machine, then the slow performance comes back. I look at the top program all the time, and see that my program only occupy 0.5% of mem and 1% of CPU, and my linux does not run anything heavy at all.

Could anyone suggest me the reason, and possible solution?

Thank you for reading.

SG.
StarGhost is offline   Reply With Quote
Old 12-27-2007   #2 (permalink)
coopstah13
Linux Enthusiast
 
coopstah13's Avatar
 
Join Date: Nov 2007
Location: MA, USA
Posts: 591
my guess is it has to do with the disk caching, most of your code is IO on the hard disk, so it stores what the contents of those files were in teh cache, that is why its faster for a little while, then slow after waiting/restarting because the cache has changed, it has to open the files again
coopstah13 is offline   Reply With Quote
Old 12-27-2007   #3 (permalink)
StarGhost
Just Joined!
 
Join Date: Nov 2006
Posts: 13
Quote:
Originally Posted by coopstah13 View Post
my guess is it has to do with the disk caching, most of your code is IO on the hard disk, so it stores what the contents of those files were in teh cache, that is why its faster for a little while, then slow after waiting/restarting because the cache has changed, it has to open the files again
Thank you coopstah13 for pointing it out.

Do you have any suggestion for me, i.e., using a different library for reading file, a method for caching the files before reading them, or using other languages such as C++ or Java with their own file I/O, or from other perspective, improve something with the hardware, etc.

SG.
StarGhost is offline   Reply With Quote
Old 12-27-2007   #4 (permalink)
coopstah13
Linux Enthusiast
 
coopstah13's Avatar
 
Join Date: Nov 2007
Location: MA, USA
Posts: 591
i don't know of any library to use personally, but you could implement something on your own I would imagine if you can't find anything, i would google around for a while, i would doubt that someone hasn't done something like that already. i don't think you're going to find switching to another language will improve any hardware performance, i would stick with what you have unless you find something in another language that implements a caching mechanism.
coopstah13 is offline   Reply With Quote
Old 12-27-2007   #5 (permalink)
wje_lf
Linux Engineer
 
wje_lf's Avatar
 
Join Date: Sep 2007
Location: Mariposa
Posts: 982
The first thing to do when trying to nail a performance problem is to correct any outstanding bugs.

I see a bug.

wordlist[] is an array of pointers. Each one points to 30 bytes after the previous one, ja?

Ok. When you use those pointers to indicate where to do your fgets(), you ask fgets() to overwrite up to 100 bytes, not 30. Is this intentional?
__________________
--
Bill

Old age and treachery will overcome youth and skill.
wje_lf is offline   Reply With Quote
Old 12-27-2007   #6 (permalink)
StarGhost
Just Joined!
 
Join Date: Nov 2006
Posts: 13
Quote:
Originally Posted by wje_lf View Post
The first thing to do when trying to nail a performance problem is to correct any outstanding bugs.

I see a bug.

wordlist[] is an array of pointers. Each one points to 30 bytes after the previous one, ja?

Ok. When you use those pointers to indicate where to do your fgets(), you ask fgets() to overwrite up to 100 bytes, not 30. Is this intentional?
Well in fact, every line in all of my files contain contains only 24 characters. So I could even reduce 30 to at most 27. The reason is: fgets reads only within particular line of a file, but if the line end before the number of char specified (e.g. 100) reached, it also terminates. Therefore in this case, even though I have specified it to be 100, but it actually reads only 24 characters, plus 2 bytes "\r\n" and the conventional ending byte, i.e. "\0". That is why there is no problem with it.

But it's good that you mentioned it, because sometimes I also mess up with the same mistake.

Regards,

SG.
StarGhost is offline   Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Different execution behaviour of a program executed from rc.local ajaymeka Linux Programming & Scripting 2 08-16-2007 06:13 AM
SATA II Slow Drive Performance. Moku Redhat / Fedora Linux Help 0 01-25-2007 11:00 AM
mail program on shell script didn't work, please advise. duke0001 Linux Programming & Scripting 1 09-22-2006 01:54 AM
SuSE 10.0 Installation Problems: freezing, rebooting, slow performance BeastInThaEast SuSE Linux Help 1 12-29-2005 01:40 PM
slow random write performance on RAID 1+0 spencerkellis Peripherals / Hardware 0 09-23-2005 09:31 PM




All times are GMT. The time now is 01:30 PM.




© 2000 - 2008 - All Rights Reserved - Property of  MAS Media

Content Relevant URLs by vBSEO 3.0.0