Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
/*
Linux ext4:
743004324000
778425979000 (+4.76%)
Win8.1 NTFS:
17565970224000
17890031536128 (+1.8%)
*/
#include <iostream>
#include <memory>
/* timing.h
*/
#ifndef TIMING_H
#define TIMING_H
#ifdef WIN32
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
typedef unsigned __int64 usCount;
static usCount GetUsCount()
{
static LARGE_INTEGER ticksPerSec;
static double scalefactor;
LARGE_INTEGER val;
if (!scalefactor)
{
if (QueryPerformanceFrequency(&ticksPerSec))
scalefactor = ticksPerSec.QuadPart / 1000000000000.0;
else
scalefactor = 1;
}
if (!QueryPerformanceCounter(&val))
return (usCount)GetTickCount() * 1000000000;
return (usCount)(val.QuadPart / scalefactor);
}
#else
#include <sys/time.h>
#include <time.h>
#include <fcntl.h>
#include <unistd.h>
typedef unsigned long long usCount;
static usCount GetUsCount()
{
#ifdef CLOCK_MONOTONIC
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
return ((usCount)ts.tv_sec * 1000000000000LL) + ts.tv_nsec * 1000LL;
#else
struct timeval tv;
gettimeofday(&tv, 0);
return ((usCount)tv.tv_sec * 1000000000000LL) + tv.tv_usec * 1000000LL;
#endif
}
#endif
#endif
int main(void)
{
usCount begin = GetUsCount(), end;
while (GetUsCount() - begin<3000000000000ULL);
begin = GetUsCount();
for (size_t n = 0; n < 1000000; n++)
{
#ifdef WIN32
auto h = CreateFile(L"test.txt", GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (!h) throw std::runtime_error("foo");
if(!CloseHandle(h)) throw std::runtime_error("foo");
#else
int h = open("test.txt", O_RDONLY);
if (h == -1) throw std::runtime_error("foo");
if (-1 == close(h)) throw std::runtime_error("foo");
#endif
}
end = GetUsCount();
usCount result1 = end - begin;
begin = GetUsCount();
for (size_t n = 0; n<1000000; n++)
{
volatile auto _h = std::make_shared<int>(5);
#ifdef WIN32
auto h = CreateFile(L"test.txt", GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (!h) throw std::runtime_error("foo");
if (!CloseHandle(h)) throw std::runtime_error("foo");
#else
int h = open("test.txt", O_RDONLY);
if (h == -1) throw std::runtime_error("foo");
if (-1 == close(h)) throw std::runtime_error("foo");
#endif
}
end = GetUsCount();
usCount result2 = end - begin;
std::cout << result1 << std::endl;
std::cout << result2 << std::endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.