Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
Bazaar
Status & Roadmap
FAQ
Authors & License
Forums
Funding Ultimate++
Search on this site
Search in forums












SourceForge.net Logo
Home » U++ Library support » U++ Core » High resolution TimeStop code
Re: High resolution TimeStop code [message #47302 is a reply to message #44773] Tue, 03 January 2017 13:10 Go to previous message
cbpporter is currently offline  cbpporter
Messages: 1428
Registered: September 2007
Ultimate Contributor
Since high resolution timer issue was not solved I switched away from TimeStop a long time ago.

I use StopWatch right now. Been using it for over a year, so I hope it is good. I'll try to merge it into TimeStop.

BTW, I'm not a native English speaker and I needed a native English speaker to tell me "WTF is a TimeStop? You mean a StopWatch?".

The answer was yes...

#ifndef __TIMESTOP_HPP__
#define __TIMESTOP_HPP__

#include <Core/Core.h>

using namespace Upp;

#ifdef PLATFORM_WIN32

class StopWatch : Moveable<StopWatch> {
	LARGE_INTEGER start;
	LARGE_INTEGER stop;
	LARGE_INTEGER freq;

public:
	double  Elapsed() {
		QueryPerformanceCounter(&stop);
		return (stop.QuadPart - start.QuadPart) * 1000.0 / freq.QuadPart;
	}

	double Seconds() {
		return (double)Elapsed() / 1000;
	}
	
	String ToString() {
		double time = Elapsed();
		return Format("%d.%03d", int(time / 1000), int(time) % 1000);
	}
	
	void Reset() {
		QueryPerformanceFrequency(&freq);
		QueryPerformanceCounter(&start);
	}

	StopWatch() {
		Reset();
	}
};

#endif

#ifdef PLATFORM_POSIX

#include <time.h>

class StopWatch : Moveable<StopWatch> {
	timespec start;

public:
	double  Elapsed() {
		timespec end;
		clock_gettime(CLOCK_REALTIME, &end);
		
		return (end.tv_sec - start.tv_sec) * 1000.0 + (end.tv_nsec - start.tv_nsec) / 1000000.0;
	}

	double Seconds() {
		return (double)Elapsed() / 1000;
	}
	
	String ToString() {
		double time = Elapsed();
		return Format("%d.%03d", int(time / 1000), int(time) % 1000);
	}
	
	void Reset() {
		clock_gettime(CLOCK_REALTIME, &start);
	}

	StopWatch() {
		Reset();
	}
};

#endif

#endif
 
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic: CoWork::Finish() can wait in a worker thread while there are jobs to do
Next Topic: Vector< Vector<int>> issue
Goto Forum:
  


Current Time: Mon Aug 25 03:56:10 CEST 2025

Total time taken to generate the page: 0.07201 seconds