Welcome to Keen Software House Forums! Log in or Sign up to interact with the KSH community.
  1. You are currently browsing our forum as a guest. Create your own forum account to access all forum functionality.

How to measure the performance impact of certain changes to ones code?

Discussion in 'Programming Questions and Suggestions' started by radar, Jun 3, 2017.

Thread Status:
This last post in this thread was made more than 31 days old.
  1. radar Trainee Engineer

    Messages:
    29
    Hey!

    I would like to know how a specific arbitrary :) change to my code impacts it's and the surrounding games performance.
    In the moment I write something up in VS and drop it in the game and look a bit at "Simulation Speed".

    I never worked with an IDE before, so my VS has only the most rudimentary setup to write some C# for SE. And I only have vague ideas about everything surrounding stack and heap and all the topics around it and for example how a specific combination of code behaves there in terms of most effective bit flipping.

    I would like to run a mod or script and look at execution time and percent kernel time or something alike, dunno :)
    Or are there maybe certain values in the Shift+F11 screens that I can use also?
    Or counting game ticks? But how and in what way?

    Any help appreciated.
     
  2. Wicorel Senior Engineer

    Messages:
    1,242
    I do something like this at the end of my main():

    Code:
    	float fper=0;
    	fper=Runtime.CurrentInstructionCount/(float)Runtime.MaxInstructionCount;
    	Echo("Instructions=" + (fper*100).ToString()+"%");
    
    And it will tell me how many instructions I am using per run.
    You could also get the run time information and display that if you want to know how many milliseconds you are taking.

    Whip has provided code that I use sometimes. You run this at the START of your main():

    Code:
    #region profiler
    // Whip's Profiler Graph Code
    int count = 1;
    int maxSeconds = 60;
    StringBuilder profile = new StringBuilder();
    void ProfilerGraph()
    {
    	if (count <= maxSeconds) // assume 1 tick per second.
    	{
    		Echo("Profiler:Add");
    		double timeToRunCode = Runtime.LastRunTimeMs;
    
    		profile.Append(timeToRunCode.ToString()).Append("\n");
    		count++;
    	}
    	else
    	{
    		Echo("Profiler:DISPLAY");
    		var screen = GridTerminalSystem.GetBlockWithName("DEBUG") as IMyTextPanel;
    		screen?.WritePublicText(profile.ToString());
    		screen?.ShowPublicTextOnScreen();
    	}
    }
    
    void resetProfiler()
    {
    	count = 1;
    	profile = new StringBuilder();
    	var screen = GridTerminalSystem.GetBlockWithName("DEBUG") as IMyTextPanel;
    	screen?.WritePublicText("");
    	screen?.ShowPublicTextOnScreen();
    
    }
    #endregion
    
    
     
  3. radar Trainee Engineer

    Messages:
    29
    Thank you so much Wicorel! :) This looks proably exactly what I was looking for. And it comes even in two flavors. You made my day with that so far.

    A follow up question came to my mind: Is this suitable also for measuring a mod? And how do I get access in the context of a mod to the result except maybe using Text Hud API/HudLcd? Something like throwing an exception in the direction of the mod debug screen (F11)?

    (Sorry for the accidental deleting and reposting, but that automerge stuff is the devils work.)
     
  4. Wicorel Senior Engineer

    Messages:
    1,242
    I believe mod API has access to a lot more things... You should ask in the mod programming forum.
     
  5. radar Trainee Engineer

    Messages:
    29
    Thanks.
     
Thread Status:
This last post in this thread was made more than 31 days old.