1. Help us improve the editor usability and artist workflows. Join our discussion to provide your feedback.
    Dismiss Notice
  2. We're looking for feedback on Unity Starter Kits! Let us know what you’d like.
    Dismiss Notice
  3. We’re giving 2017.1 beta testers a chance to win t-shirts and a Nintendo Switch. Read more on the blog.
    Dismiss Notice
  4. Unity 2017.1 beta is now available for download.
    Dismiss Notice
  5. Unity 5.6 is now released.
    Dismiss Notice
  6. Get further faster with the Unity Plus Accelerator Pack, free for new Unity Plus subscribers for a limited time. Click here for more details.
    Dismiss Notice
  7. Check out all the fixes for 5.6 on the patch releases page.
    Dismiss Notice
  8. Learn how you'll soon be able to publish your games to China in four simple steps with Xiaomi. Sign up now for early access.
    Dismiss Notice

Benchmarking Mathf with System.Math (big performance differences)

Discussion in 'Scripting' started by Stephen-Lavelle, Aug 8, 2013.

  1. Stephen-Lavelle

    Stephen-Lavelle

    Joined:
    Apr 17, 2013
    Posts:
    39
    I was quit surprised to find that Mathf.Min is way slower than System.Mathf.Min, so i did a small test suite to compare all the corresponding functions from Mathf with their System.Math counterparts (where they existed)

    all numbers are the best of 5 runs, on my 2.4ghz macbook pro (these were run in the editor - see post below by hpjohn for performance at runtime, which is slightly different but broadly similar)

    Code (csharp):
    1. ==SUMMARY==
    2.  
    3. 0.32%   MaxMin_Int      Mathf: 827610   System.Math: 267820
    4. 0.54%   Sqrt            Mathf: 1250     System.Math: 670
    5. 0.62%   Round           Mathf: 1440     System.Math: 890
    6. 0.65%   Abs_Float       Mathf: 1540     System.Math: 1000
    7. 0.66%   Abs_Int         Mathf: 2830     System.Math: 1860
    8. 0.86%   Cos             Mathf: 2170     System.Math: 1870
    9. 0.87%   Sin             Mathf: 2210     System.Math: 1930
    10. 0.89%   Ceil            Mathf: 2440     System.Math: 2170
    11. 0.90%   Atan            Mathf: 2930     System.Math: 2630
    12. 0.90%   Tan             Mathf: 3040     System.Math: 2740
    13. 0.94%   Acos            Mathf: 6050     System.Math: 5660
    14. 0.94%   Atan2           Mathf: 5248260  System.Math: 4942550
    15. 0.99%   Asin            Mathf: 5720     System.Math: 5660
    16. 0.99%   Pow             Mathf: 2533040  System.Math: 2507350
    17. 1.01%   Exp             Mathf: 6990     System.Math: 7050
    18. 1.01%   LogWithBase     Mathf: 2523020  System.Math: 2556250
    19. 1.03%   Floor           Mathf: 2200     System.Math: 2260
    20. 1.06%   Log             Mathf: 6580     System.Math: 6980
    21. 1.47%   Sign            Mathf: 1140     System.Math: 1680
    22. 1.95%   MaxMin_Float    Mathf: 1116520  System.Math: 2175540
    23.  
    24. -----
    The script's here, if anyone wants to see it/vet it/test it out for themselves

    http://pastebin.com/xeZv0BiF

    In my current game, I'm doing lots of integer-sized rectangle intersections, so switching to System.Math.Max/Min produces a pretty big speedup. (Yep, I filed a bug report about this as well).

    The main reason not to use the System.Math libraries is because they use doubles instead of floats and you have to do a bunch of casting, but even with this casting they're still sometimes faster.
     
    Last edited: Aug 8, 2013
  2. Tenebrous

    Tenebrous

    Volunteer Moderator Moderator

    Joined:
    Jul 25, 2011
    Posts:
    95
    Most of the Mathf functions call the System.Math functions with (float) casts on them, so not really surprising they can be slower, with the added function call too.
     
  3. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    1,895
    Are you running tests in-editor or in a build?
    3.2Ghz Core i3
    Editor:
    Code (csharp):
    1. 0.33%   MaxMin_Int      Mathf: 652773   System.Math: 213968
    2. 0.62%   Round       Mathf: 1046 System.Math: 645
    3. 0.71%   Abs_Float       Mathf: 1151 System.Math: 817
    4. 0.73%   Abs_Int     Mathf: 2093 System.Math: 1531
    5. 0.81%   Atan        Mathf: 1825 System.Math: 1470
    6. 0.85%   Sqrt        Mathf: 957  System.Math: 816
    7. 0.87%   Sin     Mathf: 1713 System.Math: 1486
    8. 0.90%   Acos        Mathf: 5040 System.Math: 4542
    9. 0.91%   Cos     Mathf: 1633 System.Math: 1486
    10. 0.92%   Tan     Mathf: 2134 System.Math: 1955
    11. 0.92%   Ceil        Mathf: 1416 System.Math: 1302
    12. 0.93%   Atan2       Mathf: 3486906  System.Math: 3228609
    13. 0.94%   Floor       Mathf: 1084 System.Math: 1024
    14. 0.96%   Asin        Mathf: 4772 System.Math: 4558
    15. 0.99%   Log     Mathf: 5608 System.Math: 5525
    16. 0.99%   Exp     Mathf: 6846 System.Math: 6805
    17. 1.00%   LogWithBase     Mathf: 2012234  System.Math: 2003221
    18. 1.00%   Pow     Mathf: 2114630  System.Math: 2117859
    19. 1.49%   Sign        Mathf: 906  System.Math: 1352
    20. 2.26%   MaxMin_Float        Mathf: 788378   System.Math: 1784252
    21.  
    22.  
    WebPlayer:
    Code (csharp):
    1. 0.24%   MaxMin_Int      Mathf: 276117   System.Math: 65625
    2. 0.58%   Abs_Float       Mathf: 750  System.Math: 434
    3. 0.68%   Abs_Int     Mathf: 2725 System.Math: 1866
    4. 0.70%   Round       Mathf: 622  System.Math: 433
    5. 0.80%   Floor       Mathf: 992  System.Math: 797
    6. 0.85%   Atan        Mathf: 1665 System.Math: 1413
    7. 0.88%   Cos     Mathf: 1541 System.Math: 1349
    8. 0.88%   Sin     Mathf: 1559 System.Math: 1366
    9. 0.91%   Tan     Mathf: 2134 System.Math: 1946
    10. 0.94%   Ceil        Mathf: 1133 System.Math: 1065
    11. 0.96%   Atan2       Mathf: 3179329  System.Math: 3056273
    12. 0.98%   Asin        Mathf: 4482 System.Math: 4392
    13. 0.98%   Acos        Mathf: 4514 System.Math: 4428
    14. 0.98%   LogWithBase     Mathf: 1858385  System.Math: 1823477
    15. 0.98%   Log     Mathf: 5324 System.Math: 5231
    16. 0.99%   Exp     Mathf: 6536 System.Math: 6489
    17. 1.00%   Sqrt        Mathf: 816  System.Math: 816
    18. 1.00%   Pow     Mathf: 2065306  System.Math: 2067925
    19. 1.49%   MaxMin_Float        Mathf: 587613   System.Math: 874069
    20. 1.51%   Sign        Mathf: 460  System.Math: 695
    21.  
     
  4. Stephen-Lavelle

    Stephen-Lavelle

    Joined:
    Apr 17, 2013
    Posts:
    39
    Oh, good catch. I was testing it in the editor. No substantial differences, though, it seems.

    My test code does the casting as well. Must just be the function call overhead on the trig/abs functions.
     
  5. Partel-Lang

    Partel-Lang

    Joined:
    Jan 2, 2013
    Posts:
    1,377
    Bless You!
     
  6. b1nary_atr0phy

    b1nary_atr0phy

    Joined:
    Nov 5, 2014
    Posts:
    6
    Since this is the top Google result for this topic, I'll add my 2 cents.

    It's highly probable that you aren't performing correct benchmarks in the first place. Benchmarking is not a trivial task by any means. You can't just run a function through a loop 10000 times and expect useful data in return. It requires a deep understanding of both the language, and how it's being compiled under the hood. What's more, JIT compilation further complicates matters. For example, code that is optimized in simplistic contexts will often yield varied performance and different results under complex contexts.

    If you're really serious about benchmarking, you need to do your homework first (and a lot of it.) Here's one of many great write-ups on the subject: https://www.itu.dk/people/sestoft/papers/benchmarking.pdf
     
    RavenOfCode likes this.