Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Speed testing code

Discussion in 'Scripting' started by andyz, Sep 6, 2019.

  1. andyz

    andyz

    Joined:
    Jan 5, 2010
    Posts:
    2,251
    Kind of a survey as I am not the best at speed testing code!
    To check the speed of a bit of code do you have chosen timer functions for accuracy or have a small library you use?

    Also since the editor lacks accuracy for this kind of thing and you have to do builds to accurately compare changes, do you work on it in isolation in a fresh project to reduce build time or even in a Visual studio project out of Unity?!
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,773
    The Unity Profiler is generally the best place to start. It can be used in the editor to get a broad idea of performance, and can be set up to connect to a live build to profile code remotely.

    I have a hard time imagining a scenario where profiling code from Visual Studio would provide worthwhile performance data for a Unity project.
     
  3. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    The Unity profiler is all you need really... That's the purpose of it.
     
  4. andyz

    andyz

    Joined:
    Jan 5, 2010
    Posts:
    2,251
    True you can connect the profiler but a deep profile at least will affect the performance much more than adding timers to code. It is a good starting point but do you finish there?
     
  5. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,294
    In the vast majority of cases, yes.

    Your goal is - probably - not to maximize the performance of a single bit of code. Your goal is to improve the performance of the game overall. So you go in with the profiler when the game is performing poorly, find out what's taking time, and work on that until it's good enough.

    The fact that the profiler adds overhead isn't very interesting, because you can still tell pretty well what's taking the most time.

    There's cases where I've used manual StopWatch timers - usually when I'm iterating on some slow editor function, as the editor profiler is a bit toss. But that's seldom neccessary.
     
    alexeyzakharov and Joe-Censored like this.
  6. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Yeah I use the profiler for this as well, as I'm generally not interested in how long a specific bit of code takes. What I'm interested in is what is taking an unusually large amount of time so I can focus on optimizing that.
     
    alexeyzakharov likes this.