Skip to main content
  • Home
  • Development
  • Documentation
  • Donate
  • Operational login
  • Browse the archive

swh logo
SoftwareHeritage
Software
Heritage
Archive
Features
  • Search

  • Downloads

  • Save code now

  • Add forge now

  • Help

Revision 8c90648f07f68d37b9b0ad0581e44fa069aa758a authored by unlin on 24 October 2019, 15:04:51 UTC, committed by GitHub on 24 October 2019, 15:04:51 UTC
Update Readme.md.
1 parent 401a216
  • Files
  • Changes
  • e393547
  • /
  • src
  • /
  • timer.hpp
Raw File Download

To reference or cite the objects present in the Software Heritage archive, permalinks based on SoftWare Hash IDentifiers (SWHIDs) must be used.
Select below a type of object currently browsed in order to display its associated SWHID and permalink.

  • revision
  • directory
  • content
revision badge
swh:1:rev:8c90648f07f68d37b9b0ad0581e44fa069aa758a
directory badge
swh:1:dir:4f40dcfc7f5fda95b2685f0a61d5b2bfc3a401b6
content badge
swh:1:cnt:7ba00efc03fe028ca0662716761372a37af37105

This interface enables to generate software citations, provided that the root directory of browsed objects contains a citation.cff or codemeta.json file.
Select below a type of object currently browsed in order to generate citations for them.

  • revision
  • directory
  • content
(requires biblatex-software package)
Generating citation ...
(requires biblatex-software package)
Generating citation ...
(requires biblatex-software package)
Generating citation ...
timer.hpp
//
// this is a header-only library
// in one cpp:
//   #define TIMER_IMPLEMENTATION
//   #include <timer.h>
//
// to use:
//   Timer timer;
//   timer.Start();
//   do_some_work();
//   timer.Update();
//   float deltaTime = timer.deltaTime;
//   std::string timeSinceStartString = timer.ToStdString();
//

#ifndef TIMER_H_
#define TIMER_H_

#ifdef _MSC_VER
#include <Windows.h>
#elif __APPLE__
#include <sys/time.h>
#endif

#include <string>
#include <cstdio>

class Timer
{	
public:
	Timer(void);
	void Start(long initMktime = 0);
	void Update(void);
	float DeltaTime(void);
	float timeScale;
	float TimeSinceStart(void);
	std::string ToStdString(void);
private:
	long totalMktime;
	float deltaTime;
#ifdef _MSC_VER
	float frequency;
	__int64 lastUpdate;
#elif __APPLE__
	long lastUpdate;
#else
	no_timer_implementation_available_in_this_environment
#endif
};

#endif // TIMER_H

#ifdef TIMER_IMPLEMENTATION

Timer::Timer(void) :
	timeScale(1.0f)
{	
	Start(0);
}

void Timer::Start(long initMktime)
{
	totalMktime = initMktime;
	deltaTime = 0.0f;
#ifdef _MSC_VER
	LARGE_INTEGER f;
	QueryPerformanceFrequency(&f);
	frequency = float(f.QuadPart) / 1000.0f;
	QueryPerformanceCounter(&f);
	lastUpdate = f.QuadPart;
#elif __APPLE__
	timeval time;
	gettimeofday(&time, NULL);
	lastUpdate = time.tv_sec * 1000 + time.tv_usec / 1000;
#endif
}

void Timer::Update(void)
{
#ifdef _MSC_VER
	LARGE_INTEGER t;
	QueryPerformanceCounter(&t);
	deltaTime =  (t.QuadPart - lastUpdate) / frequency * timeScale;
	totalMktime += long(deltaTime);
	deltaTime /= 1000.0f;
	lastUpdate = t.QuadPart;
#elif __APPLE__
	timeval time;
	gettimeofday(&time, NULL);
	long current = time.tv_sec * 1000 + time.tv_usec / 1000;
	deltaTime = (current - lastUpdate) * timeScale;
	totalMktime += long(deltaTime);
	deltaTime /= 1000.0f;
	lastUpdate = current;
#endif
}

std::string Timer::ToStdString(void)
{
	char buffer[18];
#ifdef _MSC_VER
	sprintf_s(buffer, "%02d:%02d:%02d'%03d",
#else
	sprintf(buffer, "%02d:%02d:%02d'%03d",
#endif
		totalMktime / 3600000 % 24,
		totalMktime / 60000 % 60,
		totalMktime / 1000 % 60,
		totalMktime % 1000);
	return buffer; // implicitly cast char* to std::string
}

float Timer::DeltaTime(void)
{
	return deltaTime;
}

float Timer::TimeSinceStart(void)
{
	return totalMktime / 1000.0f;
}

#endif //  TIMER_IMPLEMENTATION
The diff you're trying to view is too large. Only the first 1000 changed files have been loaded.
Showing with 0 additions and 0 deletions (0 / 0 diffs computed)
swh spinner

Computing file changes ...

back to top

Software Heritage — Copyright (C) 2015–2026, The Software Heritage developers. License: GNU AGPLv3+.
The source code of Software Heritage itself is available on our development forge.
The source code files archived by Software Heritage are available under their own copyright and licenses.
Terms of use: Archive access, API— Content policy— Contact— JavaScript license information— Web API