Development

#5113 (options_for_select() optimization)

You must first sign up to be able to contribute.

Ticket #5113 (closed enhancement: fixed)

Opened 7 months ago

Last modified 7 months ago

options_for_select() optimization

Reported by: Wicked Assigned to: FabianLange
Priority: minor Milestone: 1.2.1
Component: helpers Version: 1.2.0
Keywords: optimization Cc:
Qualification: Unreviewed

Description

Current options_for_select() realization checks whether each option is selected by using the in_array() function which run in O(n) time, therefore the whole complexity is estimated by O(n*m) where n = count($selected) and m = count($options). This is an overkill because this checking can be performed in just O(m) using strength of php assoc. arrays.

Also it's wise to always cast $selected to array.

The code I've been using for benchmarking is the following:

  1. Lightweight variant:
        $options = range(0, 5);
        $selected = range(0, 0); // select one out of five elements
        for ($i = 0; $i < 500; $i++) {
          options_for_select2($options, $selected);
          options_for_select($options, $selected);
        }
    
    • In this case sum of self time of my function is 297 ms, sum of inclusive is 1108ms.
    • Sum of self time of the original function is 477ms, sum of inclusive is 1304ms.
  1. Heavy variant:
        $options = range(0, 1000);
        $selected = range(500, 1000); // select 500 out of 1000 elements
        for ($i = 0; $i < 5; $i++) {
          options_for_select2($options, $selected);
          options_for_select($options, $selected);
        }
    
    • In this case sum of self time of my function is 450 ms (minimal is 72ms), sum of inclusive is 1973ms.
    • Sum of self time of the original function is 648ms (minimal is 126ms), sum of inclusive is 2470ms.

So I would estimate this overall optimization as 20%.

Attachments

patch.patch (0.9 kB) - added by Wicked on 12/01/08 05:47:40.
Optimization patch for the options_for_select()

Change History

12/01/08 05:47:40 changed by Wicked

  • attachment patch.patch added.

Optimization patch for the options_for_select()

12/01/08 05:48:06 changed by Wicked

  • type changed from defect to enhancement.

12/01/08 10:42:27 changed by FabianLange

  • owner changed from fabien to FabianLange.
  • status changed from new to assigned.

thanks for your contribution. I will double check it and commit it afterwards to all applicable versions

12/01/08 11:15:10 changed by FabianLange

  • status changed from assigned to closed.
  • resolution set to fixed.

(In [13578]) [1.2] applied performance patch from Wicket, reducing options_for_select time greatly. closes #5113

12/01/08 11:45:47 changed by FabianLange

  • milestone set to 1.2.1.

The Sensio Labs Network

Since 1998, Sensio Labs has been promoting the Open-Source software movement by providing quality web application development, training, consulting, and supporting several large Open-Source projects.