Writing into text file performance problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
tprwrsg10
Forum Newbie
Posts: 3
Joined: Wed Mar 31, 2010 10:36 am

Writing into text file performance problem

Post by tprwrsg10 »

I'm having a performance problem appending text to a text file on a server. I have 2 scripts, which almost identical, like this:

Code: Select all

<?php
$logfile = "D:/tmp/problem.log";
echo $logfile."\n";
$data = "#########@###############";
echo date("Y-m-d H:i:s")."\n";
for($i=0; $i<=1000; $i++)
{
	if($i%100==0) echo "$i ";
	error_log("$data\n", 3, $logfile);
}
echo "\n".date("Y-m-d H:i:s")."\n";
?>
Problem.log is a pre-existing 2 MB text file, of about 70K lines. I tried to run that script and it took 4:47 minutes to finish. Which is, too slow considering the hardware spec.

While the other script writes into a new, previously non-existing file. And that took just 9 seconds to finish.

This does not make sense to me, because I thought appending of text into text file shouldn't be affected by the size of the file being appended.

Some other facts:

1. I tried to write on another 10MB file, and it took just a few seconds longer than writing into non-existing file.
2. This does not happen to another 2MB file
3. Tried to run the script to append that problem.log on another computer, which is only a Pentium 4, SATA (no RAID), and took just less than 2 seconds to finish.

I've tried to use fopen, fwrite, and fclose instead of error_log, where the fopen and fclose are outside of the "for" loop, and it actually solves the performance problem. But I'm more interested in knowing what is wrong about the performance of writing that particular 2 MB file.

Anybody can give answer or opinion ?

If you need any other information, let me know and I can find out.

Thank you!
Last edited by tprwrsg10 on Wed Mar 31, 2010 11:06 am, edited 1 time in total.
tprwrsg10
Forum Newbie
Posts: 3
Joined: Wed Mar 31, 2010 10:36 am

Re: Writing into text file performance problem

Post by tprwrsg10 »

FYI, this is the spec of the server which is having the performance issue:

Windows Server 2003 Standard Edition SP 2
Intel Xeon E5540 (2.5 Ghz)
4GB of RAM

SCSI/RAID (Smart Array P410i Controller)
HP LOGICAL VOLUME SCSI Disk Device

IIS 7.0
PHP 5.2
User avatar
Weiry
Forum Contributor
Posts: 323
Joined: Wed Sep 09, 2009 5:55 am
Location: Australia

Re: Writing into text file performance problem

Post by Weiry »

is the log file currently being used or accessed during the script?

to be sure, have you tried making a copy of the problem.log to say problem2.log and running the script on the 2nd duplicate log to see if there is any difference?
tprwrsg10
Forum Newbie
Posts: 3
Joined: Wed Mar 31, 2010 10:36 am

Re: Writing into text file performance problem

Post by tprwrsg10 »

@Weiry:

Yes I have made a copy of the problem.log and the copy also have the same performance issue. And also I can be sure that the log file is being accessed by that particular script.
Post Reply