Portal Home > Knowledgebase > Articles Database > Why does this SIMPLE script run so slow??


Why does this SIMPLE script run so slow??




Posted by SDEinc, 01-07-2008, 02:31 PM
For some reason my host thinks this is a coding issue. I a few days ago my site started loading REALLY slow. So I started investigating the cause and traced it down to this simple problem. http://www.sdeonline.us/JS/slow.php My host takes minimum of 10 seconds to run a 2 line script which reads from a 3k text file. Here is what the script looks like: Taking 10-40 seconds to run this is ridiculous... Anyone know what might cause this before I go switching hosts?

Posted by fog, 01-07-2008, 02:56 PM
I'm somewhat confused as to why you're using a URL with file_get_contents, as opposed to a file path. I was going to say that it might be a problem with the "remote host" being slow, except that it's your domain and thus, presumably, on the same server. The main page (www.sdeonline.us) takes a long time to load, too. All I can imagine is that the machine is heavily overloaded. Do you have shell access (or an installation of phpSysInfo or the like) to view the server's load average? I'd wager it's quite high.

Posted by SPaReK, 01-07-2008, 03:04 PM
Try calling the file directly from the file system. I'm not sure why you are wanting to instantiate another web process to grab a file that is on the same account. Not sure if this will speed things up any, but it will certaintly make things more efficient. Instead of: $all_rows = file_get_contents('http://www.sdeonline.us/JS/index1.txt'); Use: $all_rows = file_get_contents('/home/user/path/to/index1.txt'); Last edited by SPaReK; 01-07-2008 at 03:04 PM. Reason: missing quote

Posted by SDEinc, 01-07-2008, 03:39 PM
You guys fixed that problem. But for some reason that didn't fix the slowness of my site. So I traced it down to this: \n"; foreach ($data2 as $row2) { // read the information about more attr. $all_images2 = explode(",", trim($row2[15], "{}")); //****************************************************This is the problem code below foreach ($all_images2 as $img3) { if (preg_match("/-m.jpg$/", $img3)) { $thumb3 = $img3; } } //****************************************End of problem code $thumb_size3 = getimagesize($thumb3); } ?> If I take the problem code out it runs in a split second. Why would a foreach loop cause the server to bog like that? I updated the slow script to reflect the new code. So you can see the slowness. Last edited by SDEinc; 01-07-2008 at 03:49 PM.

Posted by SDEinc, 01-07-2008, 03:42 PM
BTW here is the server load information http://www.sdeonline.us/JS/test.php

Posted by fog, 01-07-2008, 04:04 PM
I don't understand what your code does. It's especially hard without knowing what's in the file you're reading in and how it's formatted. But a few things seem odd to me: - You set $num_records2 to 1. And then you loop while $i is less than this. It seems to me that this loop would thus *always* only go through one iteration, which makes me wonder why you have a loop at all. - In your first foreach loop, you pass a "{}" to explode() as the third parameter, which is the 'limit' for how many items to explode. I don't understand what this is for. (Note that I don't work with explode() much, so it may well be valid.) Again, I really have no idea what your code is doing, so it's hard to actually diagnose. My gut says that your 'troublesome' foreach loop is looping a lot more than you expect, possibly because of problems in the code above. You could test my theory by (temporarily) commenting out the 'problem code' block and replacing it with: And seeing how much output you get. Obviously, don't do this on your main page, but on a copy of the code, since rather than doing what it should, it'll just print out a line with every loop iteration. As an aside, if you paste more code, could you put it inside of [ code ] tags for readability? (There also exists a color-coded [ php ] tag, but it doesn't seem to indent.)

Posted by SDEinc, 01-07-2008, 06:44 PM
Thanks for your help fog. After doing some experimenting I found out that that code wasn't the problem. When I took that code out the data flow stoped and thus this didn't execute: $thumb_size = getimagesize($thumb); For some reason the server I am on has serious lag time when trying to communicate with other servers as you can see by this code trying to pull from 2 different servers. http://www.sdeonline.us/JS/slow_test.php What could be causing this. My hosting guy is being a chode and not helping me solve this! So thanks again for your help!

Posted by foobic, 01-07-2008, 07:33 PM
Is it possible that the remote servers have connection limits in place or other measures to deliberately slow down connections like yours and free up bandwidth for their own visitors? Or maybe your host is throttling these connections - the transfer you use this way probably isn't accounted for in your plan. Ultimately, whenever you go to fetch content off the web you'll be at the mercy of all the connections between your server and theirs. If you want your site to be responsive, cache the content locally.

Posted by SPaReK, 01-07-2008, 07:56 PM
My bet would be on slow DNS servers or perhaps the local server (the one hosting sdeonline.us) does not have the DNS servers configured correctly. The best way to diagnose this would probably be to run an strace from the command line: strace php /path/to/slow_test.php It should show where it is slowing down at. Outside of that, try using IP addresses instead of hostnames in your slow_test.php script. This way DNS lookups don't have to be performed and if it is a DNS problem, then you will have identified the problem.

Posted by Adam-AEC, 01-07-2008, 08:57 PM
Possibly not related, but have you thought of using strpos() or strstr() instead of preg_match() ? It's my understanding that preg_match() is a great deal slower than those other 2 functions. Your regexp string is fairly simple, so I don't see why you couldn't.

Posted by SDEinc, 01-08-2008, 03:06 PM
Well I had to switch hosting. I must admit that the tech support for innohosting is HORRIBLE. They don't give a damn about keeping customers happy. I guess you get what you pay for. Cheap service = bad customer support. Never again for me. I switched to iHubNet and they are awesome. They had me up and running in about 2 hours completely migrated. Execellent response time.



Was this answer helpful?

Add to Favourites Add to Favourites    Print this Article Print this Article

Also Read
oktagone down? [Merged] (Views: 1784)
fdc down (Views: 649)
supportsages review (Views: 663)

Language: