Created
July 11, 2015 12:42
-
-
Save patniemeyer/bf73e0e6f06a8b6de97e to your computer and use it in GitHub Desktop.
Trivial Swift and Java Timing facility...
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* new Time() creates a running timer | |
* reporting on a running timer is fine | |
* each run() resets the start time and each stop() after run() accumulates time | |
* | |
* e.g. | |
* time = new Time() | |
* ... | |
* time.ms(); | |
* | |
* OR | |
* | |
* time = new Time(); | |
* ... | |
* time.run() | |
* ... | |
* time.stop() /// repeat run() ... stop() | |
* ... | |
* time.ms() | |
* OR | |
* time.toString() | |
* | |
* Note: This is obviously not thread safe. | |
* | |
* This is public domain code. | |
* @author pat | |
*/ | |
public class Time | |
{ | |
// if -1, elapsed has it all, else elapsed + now()-start | |
volatile public long start = -1; | |
volatile long elapsed; | |
public String name; | |
protected Time( long elapsed ) { | |
this.elapsed = elapsed; | |
} | |
public Time( String name ) | |
{ | |
this(); | |
this.name = name; | |
} | |
public Time() { | |
run(); | |
} | |
/** | |
* run()/stop() accumulate time | |
*/ | |
public void run() | |
{ | |
start=now(); | |
} | |
/** | |
* run()/stop() accumulate time | |
*/ | |
public void stop() | |
{ | |
elapsed += now() - start; | |
start=-1; | |
} | |
public long ms() | |
{ | |
if ( start == -1 ) { | |
return elapsed; | |
} else { | |
return ( now() - start ) + elapsed; | |
} | |
} | |
public float s() | |
{ | |
return 1.0f * ms() / 1000; | |
} | |
public static long now() { | |
return System.currentTimeMillis(); | |
} | |
public boolean gt( long ms ) { | |
return ms() > ms ; | |
} | |
/** | |
* Get a new Time instance that is the time divided over the number specified. | |
* e.g. time.per( numUsers ) might represent the time consumed by each user operation. | |
* @param num | |
* @return | |
*/ | |
public Time per( int num ) | |
{ | |
return new Time( ms() / num ); | |
} | |
/** | |
* e.g. given num iterations returns the rate in iterations per second. | |
*/ | |
public Float rate( int num ) { | |
return num/s(); | |
} | |
public String toString() | |
{ | |
if ( ms() < 1000 ) { | |
return ms()+"ms"; | |
} else | |
{ | |
return String.format( "%.1fs", s() ); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
/** | |
* new Time() creates a running timer | |
* reporting on a running timer is fine | |
* each run() resets the start time and each stop() after run() accumulates time | |
* | |
* e.g. | |
* time = new Time() | |
* ... | |
* time.ms(); | |
* | |
* OR | |
* | |
* time = new Time(); | |
* ... | |
* time.run() | |
* ... | |
* time.stop() /// repeat run() ... stop() | |
* ... | |
* time.ms() | |
* OR | |
* time.toString() | |
* | |
* Note: This is obviously not thread safe. | |
* | |
* This is public domain code. | |
* @author pat | |
*/ | |
public class Time : CustomStringConvertible | |
{ | |
// if -1, elapsed has it all, else elapsed + now()-start | |
/*volatile*/ public var start : UInt64 = 0 | |
/*volatile*/ var elapsed : UInt64 = 0 | |
public var name : String! | |
internal init( _ _elapsed : UInt64 ) { | |
self.elapsed = _elapsed; | |
} | |
public convenience init( _ name : String ) | |
{ | |
self.init(); | |
self.name = name; | |
} | |
public init() { | |
run(); | |
} | |
/** | |
* run()/stop() accumulate time | |
*/ | |
public func run() | |
{ | |
start=Time.now(); | |
} | |
/** | |
* run()/stop() accumulate time | |
*/ | |
public func stop() | |
{ | |
elapsed += Time.now() - start; | |
start = 0 | |
} | |
public func ms() -> UInt64 | |
{ | |
if ( start == 0 ) { | |
return elapsed; | |
} else { | |
return ( Time.now() - start ) + elapsed; | |
} | |
} | |
public func s() -> Float | |
{ | |
return Float(ms()) / 1000; | |
} | |
public class func now() -> UInt64 { | |
//return System.currentTimeMillis(); | |
return UInt64( NSDate().timeIntervalSince1970*1000 ) | |
} | |
public func gt( _ms : UInt64 ) -> Bool { | |
return ms() > _ms ; | |
} | |
/** | |
* Get a new Time instance that is the time divided over the number specified. | |
* e.g. time.per( numUsers ) might represent the time consumed by each user operation. | |
* @param num | |
* @return | |
*/ | |
public func per( num : Int ) -> Time | |
{ | |
return Time( ms() / UInt64(num) ); | |
} | |
/** | |
* e.g. given num iterations returns the rate in iterations per second. | |
*/ | |
public func rate( num: Int ) -> Float { | |
return Float(num)/s() | |
} | |
public func toString() -> String | |
{ | |
if ( ms() < 1000 ) { | |
return "\(ms())ms" | |
} else | |
{ | |
return NSString( format: "%.1fs", s() ) as String | |
} | |
} | |
public var description: String { | |
return toString() | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment