Re: looping eating 100 per cpu

From: André Nęss (andrena.spamreallysucks_at_ifi.uio.no)
Date: 12/04/03


Date: Thu, 04 Dec 2003 15:59:18 +0000

Juha Suni:

> kaptain kernel wrote:
>> i've got a while loop thats iterating through a text file and pumping
>> the contents into a database. the file is quite large (over 150mb).
>>
>> the looping causes my CPU load to race up to 100 per cent. Even if i
>> remove the mysql insert query and just loop through the file , it
>> still hits 100 per cent CPU. This has the knock on effect of slowing
>> my script down so that mysql inserts are occuring every 1/2 second or
>> so.
>>
>>
>>
>> here's my script:
>>
>> $fd=fopen("bigtextfile.txt","r");
>>
>> while (!feof($fd) )
>> {
>> $line=fgets($fd,4096);
>> $linecontents=explode(":",$line);
>> mysql_query("INSERT INTO MyDatabase VALUES
>> '','$linecontents[0]','$linecontents[1]'");
>> }
>
> You should try using the built-in functions of PHP to simplify this,
> could propably speed up the process quite a bit. (see
> http://www.php.net/file). I am assuming that doing it your way means
> reading the file _very_ many times, whereas with the example below the
> whole file is read once, and then processed line by line from memory.

Ehm... that means reading a 150MB file into memory, even though you don't
need it there. Not a very good idea! His code only reads the file once, and
only 4KB a time, so there is very little wasted memory.

André Nęss



Relevant Pages

  • Re: looping eating 100 per cpu
    ... > i've got a while loop thats iterating through a text file and pumping the ... > the looping causes my CPU load to race up to 100 per cent. ... data set to be used in next loop ...
    (comp.lang.php)
  • Re: looping eating 100 per cpu
    ... > i've got a while loop thats iterating through a text file and pumping ... > the looping causes my CPU load to race up to 100 per cent. ... > remove the mysql insert query and just loop through the file, ... > here's my script: ...
    (comp.lang.php)
  • Re: Why does the stack have a fixed size?
    ... The high-load inefficiencies are more a problem of CPU load than memory load, at least that's been my impression. ... So you could turn off all signals and then have one thread fetch them the clean way. ... However, if the stack is heap-based, you allocate just the same number of pages overhead for memory management). ...
    (comp.lang.functional)
  • Re: Fedora Core 3, all 1.5Gb memory used, how to free it?
    ... > and for the application to become unresponsive, due to lack of memory? ... > get more information on what was going on with system resources. ... caused by CPU load, disk access, or by swap. ...
    (alt.os.linux)
  • Re: CPU load in wince 5.0
    ... For the CPU load u can use the Remote Performance Tool. ... this via an API u can use the CreateToolhelp32Snapshot, Thread32First() ... If you mean the size of the USB Memory device then you ...
    (microsoft.public.windowsce.embedded)