/// <summary> /// Create Results from a List of execution times. /// </summary> /// <param name="executionTimes">Execution times. Results does not create a copy, so the list should not be changed.</param> internal Results(ListSnapshots <MyTimeSpan> executionTimes) { UnsortedResults = executionTimes.immutable(); Count = UnsortedResults.Count; Lazy_SortedResults = new Lazy <ReadOnlyList <MyTimeSpan> >(() => { executionTimes.mutable().Sort((MyTimeSpan first, MyTimeSpan second) => { return(Math.Sign((first - second).Ticks)); }); return(new ReadOnlyList <MyTimeSpan>(executionTimes.immutable())); }); Lazy_Total = new Lazy <MyTimeSpan>(() => { MyTimeSpan sum = new MyTimeSpan(0); foreach (MyTimeSpan result in UnsortedResults) { sum += result; } return(sum); }); Lazy_Mean = new Lazy <MyTimeSpan>(() => { return(new MyTimeSpan(Total.Ticks / Count)); }); Lazy_Median = new Lazy <MyTimeSpan>(() => SortedResults_Interpolate((decimal)Count / 2 - 0.5m)); Lazy_FirstQuartile = new Lazy <MyTimeSpan>(() => SortedResults_Interpolate((decimal)Count / 4 - 0.5m)); Lazy_ThirdQuartile = new Lazy <MyTimeSpan>(() => SortedResults_Interpolate((decimal)Count * 3 / 4 - 0.5m)); }
private void CubeGrid_OnBlockRemoved(IMySlimBlock obj) { IMyTerminalBlock asTerm = obj.FatBlock as IMyTerminalBlock; if (asTerm == null) { return; // only track IMyTerminalBlock } lock_CubeBlocks.AcquireExclusive(); try { MyObjectBuilderType myOBtype = asTerm.BlockDefinition.TypeId; string definition = asTerm.DefinitionDisplayNameText; ListSnapshots <Ingame.IMyTerminalBlock> setBlocks_Type = CubeBlocks_Type[myOBtype]; ListSnapshots <Ingame.IMyTerminalBlock> setBlocks_Def = CubeBlocks_Definition[definition]; //// replace dirty list //if (!setBlocks_Def.IsClean) //{ // setBlocks_Def = new ListCacher<Ingame.IMyTerminalBlock>(setBlocks_Def); // CubeBlocks_Definition[definition] = setBlocks_Def; //} //if (!setBlocks_Type.IsClean) //{ // setBlocks_Type = new ListCacher<Ingame.IMyTerminalBlock>(setBlocks_Type); // CubeBlocks_Type[myOBtype] = setBlocks_Type; //} setBlocks_Type.mutable().Remove(asTerm); setBlocks_Def.mutable().Remove(asTerm); } catch (Exception e) { alwaysLog("Exception: " + e, "CubeGrid_OnBlockAdded()", Logger.severity.ERROR); } finally { lock_CubeBlocks.ReleaseExclusive(); } }
private void CubeGrid_OnBlockAdded(IMySlimBlock obj) { IMyTerminalBlock asTerm = obj.FatBlock as IMyTerminalBlock; if (asTerm == null) { return; // only track IMyTerminalBlock } lock_CubeBlocks.AcquireExclusive(); try { MyObjectBuilderType myOBtype = asTerm.BlockDefinition.TypeId; string definition = asTerm.DefinitionDisplayNameText; //log("adding " + termType + " : " + definition, "CubeGrid_OnBlockAdded()", Logger.severity.TRACE); ListSnapshots <Ingame.IMyTerminalBlock> setBlocks_Type; ListSnapshots <Ingame.IMyTerminalBlock> setBlocks_Def; if (!CubeBlocks_Type.TryGetValue(myOBtype, out setBlocks_Type)) { //log("new lists for " + asTerm.DefinitionDisplayNameText, "CubeGrid_OnBlockAdded()", Logger.severity.TRACE); setBlocks_Type = new ListSnapshots <Ingame.IMyTerminalBlock>(); CubeBlocks_Type.Add(myOBtype, setBlocks_Type); } if (!CubeBlocks_Definition.TryGetValue(definition, out setBlocks_Def)) { setBlocks_Def = new ListSnapshots <Ingame.IMyTerminalBlock>(); CubeBlocks_Definition.Add(definition, setBlocks_Def); addKnownDefinition(definition); } ////log("checking for dirty lists: " + asTerm.DefinitionDisplayNameText, "CubeGrid_OnBlockAdded()", Logger.severity.TRACE); //// replace dirty list //if (!setBlocks_Def.IsClean) //{ // setBlocks_Def = new ListCacher<Ingame.IMyTerminalBlock>(setBlocks_Def); // CubeBlocks_Definition[definition] = setBlocks_Def; //} //if (!setBlocks_Type.IsClean) //{ // setBlocks_Type = new ListCacher<Ingame.IMyTerminalBlock>(setBlocks_Type); // CubeBlocks_Type[myOBtype] = setBlocks_Type; //} //log("adding: " + asTerm.DefinitionDisplayNameText + ", termType = " + myOBtype, "CubeGrid_OnBlockAdded()", Logger.severity.TRACE); setBlocks_Type.mutable().Add(asTerm); setBlocks_Def.mutable().Add(asTerm); } catch (Exception e) { alwaysLog("Exception: " + e, "CubeGrid_OnBlockAdded()", Logger.severity.ERROR); } finally { lock_CubeBlocks.ReleaseExclusive(); } }
/// <summary> /// Time an Action /// </summary> /// <param name="action">Action to perform</param> /// <param name="iterations">Number of iterations of action</param> /// <param name="ignoreFirst">Perform an extra invokation first, that will not be timed.</param> /// <returns>Results of timing</returns> /// <exception cref="ArgumentNullException">If action == null</exception> /// <exception cref="ArgumentOutOfRangeException">If iterarions < 1</exception> public static Results Time(Action action, int iterations = 1, bool extraFirst = false) { VRage.Exceptions.ThrowIf <ArgumentNullException>(action == null, "action"); VRage.Exceptions.ThrowIf <ArgumentOutOfRangeException>(iterations < 1, "iterations < 1"); if (extraFirst) { action.Invoke(); } ListSnapshots <MyTimeSpan> unsortedResults = new ListSnapshots <MyTimeSpan>(); ReadOnlyList <MyTimeSpan> mutable = unsortedResults.mutable(); for (int i = 0; i < iterations; i++) { MyGameTimer actionTimer = new MyGameTimer(); action.Invoke(); mutable.Add(actionTimer.Elapsed); } return(new Results(unsortedResults)); }