Skip to content
Permalink
Browse files

Stopwatch - GetsElapsedTime

  • Loading branch information...
bobbymcr committed May 25, 2019
1 parent 3dee9d8 commit e4ed5b324a113ae7f74cd41b30fff255cbad1679
@@ -0,0 +1,20 @@
#include "Stopwatch.h"

using namespace std::chrono;
using namespace Words;

steady_clock::time_point now()
{
return high_resolution_clock::now();
}

Stopwatch::Stopwatch()
: start_(now())
{
}

float Stopwatch::elapsed() const
{
long long ns = (now() - start_).count();
return ns / 1000000000.0f;
}
@@ -0,0 +1,17 @@
#pragma once

#include <chrono>

namespace Words
{
class Stopwatch
{
public:
Stopwatch();

float elapsed() const;

private:
std::chrono::steady_clock::time_point start_;
};
}
@@ -73,11 +73,13 @@
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="LetterBoxStr.cpp" />
<ClCompile Include="Stopwatch.cpp" />
<ClCompile Include="Str.cpp" />
<ClCompile Include="StrTrie.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="LetterBoxStr.h" />
<ClInclude Include="Stopwatch.h" />
<ClInclude Include="Str.h" />
<ClInclude Include="StrTrie.h" />
</ItemGroup>
@@ -0,0 +1,25 @@
#include "CppUnitTest.h"
#include "Stopwatch.h"
#include <chrono>
#include <thread>

using namespace Microsoft::VisualStudio::CppUnitTestFramework;
using namespace std;
using namespace std::chrono;

namespace Words
{
TEST_CLASS(StopwatchTest)
{
public:
TEST_METHOD(GetsElapsedTime)
{
Stopwatch watch;

this_thread::sleep_for(milliseconds(1));
float secs = watch.elapsed();

Assert::IsTrue(secs > 0.0f);
}
};
}
@@ -81,6 +81,7 @@
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="LetterBoxStrTest.cpp" />
<ClCompile Include="StopwatchTest.cpp" />
<ClCompile Include="StrTest.cpp" />
<ClCompile Include="StrTrieTest.cpp" />
</ItemGroup>

0 comments on commit e4ed5b3

Please sign in to comment.
You can’t perform that action at this time.