Why i cant use stopwatch class?
there are analogs ?
Solved Stopwatch class usage?
Discussion in 'Rust Development' started by bazuka5801, Jun 22, 2016.
-
Wulf Community Admin
Which class would that be? If you need a timer, you can use Oxide's Timer API, which is documented in the Docs and our GitHub repo.
-
I need to get a time start function then Current time - time start function = get time call function
-
Hey Wulf,
long time, im back at modding
I agree with the original poster here, the Timer API you mentioned seems fit for creating timed triggered events.
What he, and I, am looking for is the "performance counters" to measure execution time.
The one he mentioned "stopwatch" is used by Rust itself which is in Systems.Diagnostics.
Can it be added to the list of allowed classes inside a plugin please?
Example of it is below, but it gives the following sort of error at run time.
Code:[Oxide] 20:10 [Error] Failed to call hook 'cmdExample' on plugin 'ExamplePlugin v1.0.0' (UnauthorizedAccessException: System access is restricted, you are not allowed to use System.Diagnostics.Stopwatch)
Sample of using a Stopwatch class in C#
Code:System.Diagnostics.Stopwatch stopwatch = new System.Diagnostics.Stopwatch();while( someCondition!=true ) { // Start performance timer stopwatch.Start(); // Do some really heavy lifting here HeavyCodeHere(); // Print out how long that task took... msg = "Searching Took: " + ((stopwatch.Elapsed.TotalMilliseconds / 1000.0)).ToString() + " seconds" ; Puts( msg ); SendChatMessage(searchingPlayer, msg ) ; // Reset and restart the performance counter stopwatch.Stop(); stopwatch.Reset(); stopwatch.Start(); }
-
@bazuka5801
Here is a quick and dirty solution to this problem, allows for inaccurate timing to be recorded.
Use it just like in the example above.
Code:// Basic inaccurate implementation of System.Diagnostics.Stopwatch class PoormansStopwatch { // Here so it matches the System.Diagnostics structure internal class tElapsed { public long tickstart = 0 ; public long TotalMilliseconds { get { return (DateTime.Now.Ticks - tickstart)/TimeSpan.TicksPerMillisecond ; } } } public tElapsed Elapsed = new tElapsed(); public void Start() { Elapsed.tickstart = DateTime.Now.Ticks ; } public void Stop() { } public void Reset() { Elapsed.tickstart = DateTime.Now.Ticks ; } }
-
Code:public static class StopwatchUtils { static Dictionary<string, Stopwatch> watches = new Dictionary<string, Stopwatch>(); /// <summary> /// Start Stopwatch /// </summary> /// <param name="name">KEY</param> public static void StopwatchStart(string name) { watches[name] = Stopwatch.StartNew(); } /// <summary> /// Get Elapsed Milliseconds /// </summary> /// <param name="name">KEY</param> /// <returns></returns> public static long StopwatchElapsedMilliseconds(string name) => watches[name].ElapsedMilliseconds; /// <summary> /// Remove StopWatch /// </summary> /// <param name="name"></param> public static void StopwatchStop(string name) { watches.Remove(name); } }
-
Wulf Community Admin
-
Yeah I thought you might have either given up or made a nice solution - for others following, making a new Oxide DLL isnt always possible / easy for your end users if you are just writing a simple mod. I hope @Wulf can add the above as I think its useful.
-
Wulf Community Admin
-
Code:while (someCondition != true) { // Start time var started_at = Time.realtimeSinceStartup; // Do some really heavy lifting here HeavyCodeHere(); // Print out how long that task took... var msg = $"Searching Took: {Time.realtimeSinceStartup - started_at:0.000} seconds."; Puts(msg); SendChatMessage(searchingPlayer, msg ) ; }
-
Building off of what Mughisi wrote:
Here is a quick class that should act like System.Diagnostics.Stopwatch using Unity Time
Code:class UStopWatch { float startTime; int startFrame; float stopTime; int stopFrame; bool _isRunning; public bool isRunning { get { return _isRunning; } private set { _isRunning = value; } } public float elapsedSeconds { get { return (isRunning ? Time.realtimeSinceStartup : stopTime) - startTime; } } public int elapsedMilliseconds { get { return Mathf.RoundToInt(elapsedSeconds / 1000f); } } public int elapsedFrames { get { return (isRunning ? Time.frameCount : stopFrame) - startFrame; } } public UStopWatch(bool startNow = true) { if(startNow) Start(); } public void Start() { Reset(); _isRunning = true; } public void Stop() { stopTime = Time.realtimeSinceStartup; stopFrame = Time.frameCount; _isRunning = false; } public void Reset() { startTime = Time.realtimeSinceStartup; startFrame = Time.frameCount; stopTime = Time.realtimeSinceStartup; stopFrame = Time.frameCount; } }
-
Wulf Community Admin
The biggest concern that @Mughisi brought up about exposing the Stopwatch, is that plugins would need to make sure they stop them, otherwise it could get a little hairy.
-
-
Wulf Community Admin
-
[DOUBLEPOST=1483658778][/DOUBLEPOST] -
Wulf Community Admin
@Lucien, our only concern is that plugins may causing some issues if they start messing around with threading. Do you see this being an issue with System.Threading.Interlocked being exposed? With the many alternatives, we're not sure it's worth exposing it. -
how use // Reference To my dll extension?
-
Wulf Community Admin
-
-
The advanced people that want to use stopwatch can follow an Ext route to get what ever functionality they want?