473,378 Members | 1,140 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

looping eating 100 per cpu

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]'");
}
Jul 17 '05 #1
8 4027
"kaptain kernel" <no****@nospam.gov> wrote in message
news:3f***********************@news.easynet.co.uk. ..
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]'");
}


Thats because you are asking it to do alot.

Regards
Richard Grove

http://shopbuilder.org - ecommerce systems
Become a Shop Builder re-seller:
http://www.affiliatewindow.com/affil...ls.php?mid=611
http://www.affiliatewindow.com/a.pl?590

Jul 17 '05 #2
kaptain kernel:
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]'");
}


That's very strange, 1/2 second between each insert is crazy. Is it possible
that you're running out of memory? (It shouldn't, as you only read 4KB on
each iteration). The 100% CPU usage isn't surprising though.

What if you remove the query and don't do anything with the data? What if
you write a perl or python script which does the same (again, without the
query)?

André Nęss
Jul 17 '05 #3
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]'");
}


I wonder if it would be faster with fread()? If the average line is very small
it should drastically reduce the number of times the thing has to read from the
disk. This is UNTESTED code, so if you use it, please be careful. I don't read
from huge files often enough to be confident that it will work.

$line = "";

while ($line.=fread($fd,4096))
{
$insertlist = "";
$linecontents=explode("\n",$line);
$line = $linecontents[count[$linecontents]]; // remember beginning of last
(incomplete) data set to be used in next loop
unset($linecontents[count[$linecontents]]); // delete last (incomplete) data
set
foreach($linecontents as $val) {
$insertlist .= "('','$val[0]','$val[1]'),"; // build the data portion of the
mysql query statement
}

// insert up to 4K worth of data at once while removing trailing comma
mysql_query("INSERT INTO MyDatabase (col1, col2, col3) VALUES " .
rtrim($insertlist, ","));
}

Regards,
Shawn

--
Shawn Wilson
sh***@glassgiant.com
http://www.glassgiant.com
Jul 17 '05 #4
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.

$bigfile = file("bigtextfile.txt");

foreach ($bigfile as $line_num => $line) {
$linecontents=explode(":",$line);
mysql_query("INSERT INTO MyDatabase VALUES
'','$linecontents[0]','$linecontents[1]'");
}

HTH

--
Suni

Jul 17 '05 #5
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
Jul 17 '05 #6
Juha Suni wrote:

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.

$bigfile = file("bigtextfile.txt");

foreach ($bigfile as $line_num => $line) {
$linecontents=explode(":",$line);
mysql_query("INSERT INTO MyDatabase VALUES
'','$linecontents[0]','$linecontents[1]'");
}


Excerpt from http://ca.php.net/manual/en/function.file.php:

//------------------------------------------
Note: Now that file() is binary safe it is 'much' slower than it used to be. If
you are planning to read large files it may be worth your while using fgets()
instead of file() For example:

$fd = fopen ("log_file.txt", "r");
while (!feof ($fd))
{
$buffer = fgets($fd, 4096);
$lines[] = $buffer;
}
fclose ($fd);

The resulting array is $lines.

I did a test on a 200,000 line file. It took seconds with fgets() compared to
minutes with file().
//------------------------------------------

My guess is, for a 150Mb file, file() would take forever, if it worked at all.

Shawn
--
Shawn Wilson
sh***@glassgiant.com
http://www.glassgiant.com
Jul 17 '05 #7
> //------------------------------------------
Note: Now that file() is binary safe it is 'much' slower than it used
to be. If you are planning to read large files it may be worth your
while using fgets() instead of file() For example:

$fd = fopen ("log_file.txt", "r");
while (!feof ($fd))
{
$buffer = fgets($fd, 4096);
$lines[] = $buffer;
}
fclose ($fd);

The resulting array is $lines.

I did a test on a 200,000 line file. It took seconds with fgets()
compared to minutes with file().
//------------------------------------------

My guess is, for a 150Mb file, file() would take forever, if it
worked at all.

Shawn


Thank you for the info, I stand corrected.

I guess You really have no good options on this front. a 150 MB
text-file is a huge load of data, and looping through it just seems to
be too much for your system.

First I would consider the source of the text-file, where does it come
from? Is there no way the application creating the file could push the
data directly to mysql? I would also consider benchmarking with the
original data in different formats. How fast is the script freezing,
i.e. is it slow from the start or only after reading the first x lines?
Could it be faster to process 10 x 15Mb text-files than a large single
file?

Other than that, I cant really guess. I suppose a script with another
(compiled) language might be faster, so you could try C. A PHP-optimizer
could also fasten the process quite a bit.

If nothing else helps, a system hardware upgrade might be your best
option, considering the importance of the script, of course.

--
Suni
Jul 17 '05 #8
Just out of curiosity I tried looping through a 150 meg text files with 100
byte records (i.e. ~1,500,000 lines) using fgets(). My computer cut through
it in less than 2 minutes.

My thinking is that the SQL statements generated by the script are malformed
most of the time. That would explain why it takes so long for an actual
insert to occur. If an insert occurs on every iteration through the loop,
the MySQL process would be using up most of the CPU time instead, with
Apache/PHP sitting there waiting for it to finish each query.

Uzytkownik "Shawn Wilson" <sh***@glassgiant.com> napisal w wiadomosci
news:3F***************@glassgiant.com...
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]'");
}
I wonder if it would be faster with fread()? If the average line is very

small it should drastically reduce the number of times the thing has to read from the disk. This is UNTESTED code, so if you use it, please be careful. I don't read from huge files often enough to be confident that it will work.

$line = "";

while ($line.=fread($fd,4096))
{
$insertlist = "";
$linecontents=explode("\n",$line);
$line = $linecontents[count[$linecontents]]; // remember beginning of last (incomplete) data set to be used in next loop
unset($linecontents[count[$linecontents]]); // delete last (incomplete) data set
foreach($linecontents as $val) {
$insertlist .= "('','$val[0]','$val[1]'),"; // build the data portion of the mysql query statement
}

// insert up to 4K worth of data at once while removing trailing comma
mysql_query("INSERT INTO MyDatabase (col1, col2, col3) VALUES " .
rtrim($insertlist, ","));
}

Regards,
Shawn

--
Shawn Wilson
sh***@glassgiant.com
http://www.glassgiant.com

Jul 17 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

45
by: Trevor Best | last post by:
I did a test once using a looping variable, first dimmed as Integer, then as Long. I found the Integer was quicker at looping. I knew this to be true back in the 16 bit days where the CPU's (80286)...
5
by: masood.iqbal | last post by:
My simplistic mind tells me that having local variables within looping constructs is a bad idea. The reason is that these variables are created during the beginning of an iteration and deleted at...
1
by: Diva | last post by:
Hi, I have a data grid in my application. It has 20 rows and I have set the page size as 5. I have a Submit button on my form and when I click on Submit, I need to loop through the rows in the...
2
by: Bob | last post by:
I'm sure some of you are familliar with PTFB... http://www.bobos.demon.co.uk/par/PTFB.htm it's a wonderful little app that answers dialogue box questions for you when they pop up. I have...
2
by: Just Me | last post by:
I do not get WM_KEYDOWN messages for arrow keys presses in my overridden WndProc for a picturebox control. Something must be eating them - anyone familiar with what's going on? Thanks
1
by: google | last post by:
Hello all- Apologies to everyone for what's probably a very inchoate and uninformed question, but I've been thrust into the position about having to learn more about PHP very quickly. (I'm also...
9
by: Tomi Lindberg | last post by:
Hi, With the following function definition, is it possible to create an instance of class C outside the function f (and if it is, how)? And yes, I think this is one of those times when the real...
5
by: Per B. Sederberg | last post by:
Hi Everybody: I'm having a difficult time figuring out a a memory use problem. I have a python program that makes use of numpy and also calls a small C module I wrote because part of the...
20
by: Ifoel | last post by:
Hi all, Sorry im beginer in vb. I want making programm looping character or number. Just say i have numbers from 100 to 10000. just sample: Private Sub Timer1_Timer() if check1.value= 1...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.