Welcome Guest, Not a member yet? Register   Sign In
Optimizing Code
#1

[eluser]Josh Holloway[/eluser]
I'm writing code for a pretty intense website.

I've been using the profiler to check my execution times for some of the heaver load functions and I've hit a bit of a snag on one inparticular.

We're using 3D Secure to authorise debitcard payments (You know, the annoying further authentication required box from your bank asking for a password)

However, the size of the function is causing execution time to rocket to ~15 seconds.

My question is, in your experiences, is it better to use 1 large function e.g:

Code:
function some_function()
{
   //large block of code here
}

or several smaller functions

Code:
function part_of_function()
{
   //part of the whole function
}

function another_part_of_function()
{
   //another part of function
}

function 3rd_part_of_function()
{
   //final part of function
}

function call_all_functions()
{
   //some logic here
   $part_of_function();

   //some more logic here  
   $another_part_of_function();

   //even more logic here
   $3rd_part_of_function();
}

I'd post my real code but there's a lot more about (900 lines from all libraries and models that make up the process) and would take a awfully long time to cut up and post whilst keeping the post short and interesting.

thanks
#2

[eluser]Basketcasesoftware[/eluser]
One large function is usually faster than a bunch of smaller ones. Each function call adds overhead to run time. There are a set of library functions to measure how long a piece of code is taking in CI.
#3

[eluser]InsiteFX[/eluser]
There is alot more to optimizing your code.

Certain statements are faster then others.

You will need to look into optimizing your code statements also.

Here is an example:
Code:
// this is slow, see the $i < sizeof($people);
for($i = 0; $i < sizeof($people); ++$i)
{
    $people[$i]['salt'] = rand(000000, 999999);
}

// to optimize this:
$count = sizeof($people);
for($i = 0; $i < $count; ++$i)
{
    $people[$i]['salt'] = rand(000000, 999999);
}

See the above everytime it loops it has to calculate the sizeof($people).
But assigning it to a variable speeds it up.

While loops are also slow.

InsiteFX
#4

[eluser]Basketcasesoftware[/eluser]
Exactly. My reply was aimed at the original poster's example. Which is also why I suggested the use of the profiler library. Your example was an example of removing the recalculation of loop invariant code.
#5

[eluser]n0xie[/eluser]
Most of these optimisations are made redundant by using an opcode cache. If you haven't installed one already, I would do that first and see if that solves your issue.
#6

[eluser]Josh Holloway[/eluser]
I've been using the profiler to optimize the code anyway...

I've done more testing and it's caused by the API I'm using. It's taking ~10 seconds to return the request from the bank.

thanks for your input guys Smile




Theme © iAndrew 2016 - Forum software by © MyBB