Welcome Guest, Not a member yet? Register   Sign In
function preforms much worse on CI

i'm currently working on switching a site from normal old fashion code to CI, a site which includes a lot of image operations.
one of the things i noticed i that a very simple function which normally took less than a second to return an answer, now takes between 3-4 seconds which is a very big issue because it's one of the lighter functions in the code.

the function is in one of the models and is called from a controller function which is called using Ajax, i've isolated the ajax and got no difference.

another thing that i've tried was to change the address of the folder to the actual physical address on my machine, this didn't help much either.

do you have any other ideas?
thank you for your time and attention,

Maybe the old code was caching the images and with the new code you have not added any caching!


tnx for the answer, i looked up some info about caching and i can't understand how would this help speed up my function. just so we'll understand each other better, here's the function:

$path = __FILE__;            
        $path = str_replace("application\models\pic_load.php","JSandProducts\\".$type,$path);
        $count = 0;
        $handle = opendir ($path);
        $file = readdir ($handle);
        $file = readdir ($handle);
        while (false != ($file=readdir ($handle)))
            $count += 1;
        closedir ($handle);
        unset ($path,$file,$handle);
        return $count;

tnx again

well, nothing you posted has anything directly to do with the CI framework, so I don't think that's the problem. I do see a lot of code that is doing nothing in your post.

You have 2 "$file = readdir ($handle);" and even a 3rd within your while statement. I believe you just need the one in the while statement.

Are you just trying to count the files/directories in the path?
$path = str_replace("application\models\pic_load.php","JSandProducts\\".$type, __FILE__);
$files = scandir($path);
return count($files);  //you might want to subtract 2 from this because if its a valid directory it will always return "." and "..".

i am indeed trying to count the files in the path, the first 2 readdir's are for the '.'&'..'

i know that the code has nothing to do with CI that is exactly why i find it so odd that just a change of framework causes such a delay.

ok, i've tried your code and indeed it made a difference, unfortunately another function in the code is still quite slow and i can't think of a way to speed it up

function load_pics($type,$i,$page)            //returns the file names in order
        $path = str_replace("application\models\pic_load.php","JSandProducts\\".$type, __FILE__);
        $files = scandir($path);
        $index = ($page-1)*9 + $i -95;
        if (isset($files[$index]))
            return $files[$index];
            return "";

run benchmark timers.

determine which function is taking the longest.

the entire process of getting through both the controller and the model functions is reportedly taking 0.0005 seconds(although the echo output appears after 2-4 seconds).

now in the worse case there will be 2 functions running in about the same time (0.001) and they two will iterate 9 times (0.009)that still does not explain how am i getting to 7 seconds (worse case).
i reckon that the problem can't be in the ajax for the entire loop (the one which runs 9 times) is made out of 5 rows, 3 of which are the ajax and another is the index incrementation.

Profile it. Install xdebug, generate some profile data, and use cachegrind to look at what code is using up all the time. Assuming that your 7 seconds is not transfer related...

here's what i think is a VERY IMPORTANT UPDATE
after some research i found out the a rather big part of a request's response time lays with the type of the request.
according to this:
using POST actually calls the server twice, while using GET call once. trying this i can strongly recommend it for you all,

thank you so very much for your attention and time,

Theme © iAndrew 2016 - Forum software by © MyBB