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 3785980
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T11:36:42+00:00 2026-05-19T11:36:42+00:00

Which construction is faster: $a = $b * $c ? $b * $c :

  • 0

Which construction is faster:

$a = $b * $c ? $b * $c : 0;  

or

$i = $b * $c;  
$a = $i ? $i : 0;  

All variables are local ones.

Does speed differs for mulitplication, addition, substraction and division?

Update:

Here’s some clarification:

  1. This is a theoretical question about writing speed-optimized code from scratch. Not about "searching bottlenecks".
  2. I can measure code speed by myself. But it’s was not a question about homework of using microtime(). It was a question about how PHP-interpreter works (what I tried to figure out by digging google myself but was unseccusfull).
  3. Moreover – I did measuring with myself and was a little confused. Different starting values of $a, $b and $c (combinations of zeros, negative, positive, integer and floats) produce different results between constructions. So I was confused.

BoltClock provide me usefull info but user576875 made my day by posting a link to opcode decoder! His answer contains also direct answer to my question. Thanks!

  • 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-19T11:36:43+00:00Added an answer on May 19, 2026 at 11:36 am

    If you have PHP 5.3, this is faster:

    $a = $b * $c ?: 0; 
    

    This is the same as $a = $b * $c ? $b * $c : 0;, but the $a*$b calcultation is done only once. Also, it doesn’t do additional assignments as in your second solution.

    Using Martin v. Löwis’s benchmark script I get the following times:

    $a = $b * $c ?: 0;               1.07s
    $a = $b * $c ? $b * $c : 0;      1.16s
    $i = $b * $c; $a = $i ? $i : 0;  1.39s
    

    Now these are micro-optimizations, so there is probably many ways of optimizing your code before doing this 🙂

    If it is not the case, you may also want to compare generated PHP OP codes:

    1 $a = $b * $c ? $b * $c : 0; :

    number of ops:  8
    compiled vars:  !0 = $a, !1 = $b, !2 = $c
    line     #  op                           fetch          ext  return  operands
    -------------------------------------------------------------------------------
       1     0  MUL                                              ~0      !1($b), !2($c)
             1  JMPZ                                                     ~0, ->5
             2  MUL                                              ~1      !1($b), !2($c)
             3  QM_ASSIGN                                        ~2      ~1
             4  JMP                                                      ->6
             5  QM_ASSIGN                                        ~2      0
             6  ASSIGN                                                   !0($a), ~2
             7  RETURN                                                   null
    

    2 $i = $b * $c; $a = $i ? $i : 0;

    number of ops:  8
    compiled vars:  !0 = $i, !1 = $b, !2 = $c, !3 = $a
    line     #  op                           fetch          ext  return  operands
    -------------------------------------------------------------------------------
       1     0  MUL                                              ~0      !1($b), !2($c)
             1  ASSIGN                                                   !0($i), ~0
             2  JMPZ                                                     !0($i), ->5
             3  QM_ASSIGN                                        ~2      !0($i)
             4  JMP                                                      ->6
             5  QM_ASSIGN                                        ~2      0
             6  ASSIGN                                                   !3($a), ~2
             7  RETURN                                                   null
    

    3 $a = $b * $c ?: 0; :

    number of ops:  5
    compiled vars:  !0 = $a, !1 = $b, !2 = $c
    line     #  op                           fetch          ext  return  operands
    -------------------------------------------------------------------------------
       1     0  MUL                                              ~0      !1($b), !2($c)
             1  ZEND_JMP_SET                                     ~1      ~0
             2  QM_ASSIGN                                        ~1      0
             3  ASSIGN                                                   !0($a), ~1
             4  RETURN                                                   null
    

    These OP code listings was generated by the VLD extension.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing some code for a class constructor which loops through all the properties
I have a class which constructor takes a Jakarta enums . I'm trying to
I have a class ReportingComponent<T> , which has the constructor: public ReportingComponent(IQueryable<T> query) {}
Which gets called first - the base constructor or other stuff here? public class
I have a class which has the following constructor public DelayCompositeDesigner(DelayComposite CompositeObject) { InitializeComponent();
At the XmlSerializer constructor line the below causes an InvalidOperationException which also complains about
I have a structure which I create a custom constructor to initialize the members
How would I go about constructing the contour of 2d polygon which is formed
For my exception class i have a constructor that has multi arguments (...) which
Which graduate program should I choose – SUNY Buffalo or SUNY Binghamton?

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.