Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7582923
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T18:33:21+00:00 2026-05-30T18:33:21+00:00

I have a script which calls this function more than 100k times, so I

  • 0

I have a script which calls this function more than 100k times, so I am looking for anyway to squeeze a bit more performance out of it.

Can you suggest optimisations or an alternate method for calculating standard deviation in PHP?

function calcStandardDev($samples){


    $sample_count = count($samples);

    for ($current_sample = 0; $sample_count > $current_sample; ++$current_sample) $sample_square[$current_sample] = pow($samples[$current_sample], 2);

    return sqrt(array_sum($sample_square) / $sample_count - pow((array_sum($samples) / $sample_count), 2));

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T18:33:23+00:00Added an answer on May 30, 2026 at 6:33 pm

    foreach by referance is faster than for, an you already have a loop, you can calculate “sum” in this loop. and $x*$x is so faster then pow($x,2);
    there are some functions comparations. hope to help.

    Your Function microtime = ~ 0.526

    Second Function = ~ 0.290

      <?php
        function calcStandardDev($samples)
        {
    
    
            $sample_count = count($samples);
    
            for ($current_sample = 0; $sample_count > $current_sample; ++$current_sample) 
                $sample_square[$current_sample] = pow($samples[$current_sample], 2);
    
            return sqrt(array_sum($sample_square) / $sample_count - pow((array_sum($samples) / $sample_count), 2));
    
        }
    
        function calcStandardDev2($samples)
        {
            $sample_count = count($samples);
    
            $sum_sample_square  = 0;
            $sum_sample         = 0;
    
            foreach ($samples as &$sample) 
            {
                $sum_sample         += $sample;
                $sum_sample_square  += $sample * $sample; 
            }
    
            return sqrt($sum_sample_square / $sample_count - pow($sum_sample / $sample_count,2));
    
        }
    
         function calcStandardDev3($samples)
        {
            $sample_count = count($samples);
    
            $sum_sample_square  = 0;
            $sum_sample         = 0;
    
            foreach ($samples as &$sample) 
            {
                $sum_sample         += $sample;
                $sum_sample_square  += pow($sample ,2); 
            }
    
            return sqrt($sum_sample_square / $sample_count - pow($sum_sample  / $sample_count,2));
    
        }
    
        echo "<pre>";
        $samples = range(2,100000);
    
        $start  = microtime(true);
        echo calcStandardDev($samples)."\r\n";
        $end  = microtime(true);
        echo $end - $start ."\r\n";  
        echo "-------\r\n";
    
        $start  = microtime(true);
        echo calcStandardDev2($samples)."\r\n";
        $end  = microtime(true);
        echo $end - $start."\r\n";
        echo "-------\r\n";
    
        $start  = microtime(true);
        echo calcStandardDev3($samples)."\r\n";
        $end  = microtime(true);
        echo $end - $start;
        echo "-------\r\n";
    ?>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have matlab function that calls a Perl script which converts a large text
I have a Perl script which calls another script. The Perl script should be
I have this script which basically toggles a bgColor class on and off so
I have a script which creates a user-defined object like this: obj = new
I have PL/SQL function, which is programmed to sort a set of data. This
I have a script in Filemaker that calls a Brian Dunning hosted custom function
i have a python script which keeps crashing on: subprocess.call([pdftotext, pdf_filename]) the error being:
I have a Batch file which will call a Powershell Script : BATCH FILE
I have a Makefile from which I want to call another external bash script
I have a script which contacts a few sources and tell them the IP-address

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.