Current location: Hot Scripts Forums » Programming Languages » PHP » While loop makes everything slow


While loop makes everything slow

Reply
  #1 (permalink)  
Old 02-20-08, 11:52 AM
TenFormer's Avatar
TenFormer TenFormer is offline
Newbie Coder
 
Join Date: Jan 2007
Posts: 60
Thanks: 0
Thanked 0 Times in 0 Posts
X_X While loop makes everything slow

Hey again ,

I created a class, with a function.
This function needs an errorid, and then checks a language file for the error with the id. The language file looks like this
Quote:
1:Error in module filesystem structure.
2:The specified module does not exist or is not installed.
So basically what I am trying to do is, save the whole file in an array with file() method, so if I am right about this, each line is saved in a value of the array.

Now a while loop is initiated, and each line seperately is ripped apart with explode() where the ":" is.

Here is the code:
PHP Code:

    function systemerror($errorid)

    {
        while(
false !== ($line file("lang/systemerror.lang")))
        {
            
$error explode(":"$line);
            if(
$error[1] == $errorid)
            {
                
$errormessage $error[2];
                include(
"themes/core/systemerror.tpl");
            }
        }
    } 
Even though it works, I experienced this makes the whole website extremly slow, I think due to the fact that it goes through all lines. I cant really think of a while() condition which fits better. Basically it would just need to search the lines until it found a fit to the submitted errorid.

Does someone have an idea bout this?

Thank you again!


Max
Reply With Quote
  #2 (permalink)  
Old 02-20-08, 11:58 AM
Nico's Avatar
Nico Nico is offline
Community Leader
 
Join Date: Sep 2005
Location: Spain
Posts: 8,075
Thanks: 11
Thanked 88 Times in 83 Posts
It's slow because you're calling file() in every loop, which makes PHP open and read the file every time new.

Try it this way:
PHP Code:

$lines file("lang/systemerror.lang");

foreach (
$lines AS $line)
{
    
$error explode(":"$line);
    if (
$error[1] == $errorid)
    {
        
$errormessage $error[2];
        include(
"themes/core/systemerror.tpl");
        break;
    }

I also added a break;, so it stops the loop when it has found the error ID. The whole thing could still be slow, that depends on how big the file actually is.

EDIT:

And I'm guessing it should be:
PHP Code:

if ($error[0] == $errorid
?? According to your example, $error[0] should hold the error ID.


I suggest you change your system, and use arrays to store the errors and error numbers.
PHP Code:

$error[1] = 'Error in module filesystem structure.';
$error[2] = 'The specified module does not exist or is not installed.';
// ... 

And in your function you'd just do:
PHP Code:

include 'lang/systemerror.lang';

if (isset(
$errors[$errorid]))
{
    
$errormessage $errors[$errorid];
    include(
"themes/core/systemerror.tpl");
}
else
{
    
// Error ID not found

That should be significantly faster.

Last edited by Nico; 02-20-08 at 12:06 PM.
Reply With Quote
  #3 (permalink)  
Old 02-20-08, 12:07 PM
TenFormer's Avatar
TenFormer TenFormer is offline
Newbie Coder
 
Join Date: Jan 2007
Posts: 60
Thanks: 0
Thanked 0 Times in 0 Posts
works perfect, thanks alot.

And yes you where absolutely right about the error id, it is $error[0] which holds the id. Got lost in copying code here i guess

Thanks a lot again, really amazing the kind of support I get here.
Reply With Quote
  #4 (permalink)  
Old 02-20-08, 12:09 PM
Nico's Avatar
Nico Nico is offline
Community Leader
 
Join Date: Sep 2005
Location: Spain
Posts: 8,075
Thanks: 11
Thanked 88 Times in 83 Posts
You're welcome.

I edited my post above, and added a further suggestion. (Not sure if you've seen it)
Reply With Quote
  #5 (permalink)  
Old 02-20-08, 12:19 PM
TenFormer's Avatar
TenFormer TenFormer is offline
Newbie Coder
 
Join Date: Jan 2007
Posts: 60
Thanks: 0
Thanked 0 Times in 0 Posts
No I did not see that, and I see your point. I wanted to do it this way first to, but I am always stumbling upon problems with classes, which I am fairly new to.

This whole function is in a class, and I am always having huge problems with include() in classes, even if I put var in front of it.

And I actually just wanted to post about a similar problem, for example this errorhandling class, needs to be used in other classes too.

And I dont manage to load other classes within a class, and have no idea what to do.

Is there even a way, to use other classes in a class, and if yes how?

If I would use your suggested way (storing errors in an array), would it work if I simply had the language file like this?

PHP Code:

var $error[1] = "Error message"
?

Thanks,
Max
Reply With Quote
  #6 (permalink)  
Old 02-20-08, 12:27 PM
Nico's Avatar
Nico Nico is offline
Community Leader
 
Join Date: Sep 2005
Location: Spain
Posts: 8,075
Thanks: 11
Thanked 88 Times in 83 Posts
Quote:
Originally Posted by TenFormer View Post
Is there even a way, to use other classes in a class, and if yes how?
Yes, you can do it two ways:
PHP Code:

<?php

class yourclass extends otherclass
{
     
// Class code

    
function foo()
    {
        
parent::method_from_otherclass();
    }
}

?>
www.php.net/language.oop5.basic (Example #6)

Or you can do:
PHP Code:

<?php

class foo
{
    function 
bar()
    {
        
$foo = new Foo_Bar();

        
$foo->do_something();
    }
}

?>
Quote:
If I would use your suggested way (storing errors in an array), would it work if I simply had the language file like this?

PHP Code:

var $error[1] = "Error message"
No need to do that, if the error messages are in an external file. Inside your class, you can treat it like a normal array.

Let's say your external error file looks like:
PHP Code:

<?php

$error
[1] = 'Error in module filesystem structure.';
$error[2] = 'The specified module does not exist or is not installed.';

// ...

?>
You can just include it like I did in the example in my previous post.


Oh yeah, and var is for PHP 4, it does work in PHP 5, but you should start learning OOP for PHP 5 directly. (PHP 4 is officially dead)

www.php.net/language.oop5.visibility

Last edited by Nico; 02-20-08 at 12:30 PM.
Reply With Quote
  #7 (permalink)  
Old 02-21-08, 09:15 AM
TenFormer's Avatar
TenFormer TenFormer is offline
Newbie Coder
 
Join Date: Jan 2007
Posts: 60
Thanks: 0
Thanked 0 Times in 0 Posts
Allright thanks a lot. I will do it as u suggested, makes a lot of sense.
As well I bought a book for PHP5 today, hopefully that will help me understand OOP.

Thanks a lot, really awesome help
Reply With Quote
Reply

Bookmarks


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 Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
infinite loop versus guarded blocks UnrealEd Everything Java 0 11-11-07 02:53 PM
Update multiple rows outside loop - need help ElvansX PHP 1 12-03-06 01:55 AM
having Loop issues, help... Advanced todayscoffee PHP 2 02-27-06 12:36 AM
Total mysql_num_rows on while loop dihan PHP 2 08-21-05 03:31 PM
While loop jaishalg PHP 1 11-23-04 03:36 PM


All times are GMT -5. The time now is 07:37 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.