/*

File StopWatch.m

A stopwatch can be stopped, started, and reset.

*/

#import "StopWatch.h"

#import <sys/resource.h>


/* 行行行行行行行行行行行行行行行行行行行行行行行行行行行行行行行行行行行行行行  */


@implementation StopWatch

- (float) getTime		{	return time;	}


/* 行行行行行行行行行行行行行行行行行行行行行行行行行行行行行行行行行行行行行行  */


- reset
{
	time = 0.0;
	return self;
}


- start
{
	struct rusage	rusage;
	
	getrusage(RUSAGE_SELF, &rusage);
	beganAt = rusage.ru_utime.tv_sec + rusage.ru_stime.tv_sec;
	beganAt += .000001 * (rusage.ru_utime.tv_usec + rusage.ru_stime.tv_usec);
	
	return self;
}


- stop
{
	struct rusage	rusage;
	float			endAt;
	
	getrusage(RUSAGE_SELF, &rusage);
	endAt = rusage.ru_utime.tv_sec + rusage.ru_stime.tv_sec;
	endAt += .000001 * (rusage.ru_utime.tv_usec + rusage.ru_stime.tv_usec);
	time += endAt - beganAt;
	
	return self;
}


@end