-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add UUID generator based on two MT19937 pseudo-random number generato…
…rs seeded by true randomness.
- Loading branch information
Showing
4 changed files
with
71 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
#include "UuidGenerator.h" | ||
#include "Timing.h" | ||
|
||
#include <iostream> | ||
|
||
int main() | ||
{ | ||
const size_t N = 100000000; | ||
|
||
UuidGenerator generator; | ||
|
||
char uuid[16]; | ||
|
||
std::cout << std::hex; | ||
|
||
auto start = GetTimeStamp(); | ||
|
||
for (size_t i = 0; i < N; ++i) | ||
{ | ||
generator.GenerateUuid(uuid); | ||
} | ||
|
||
auto end = GetTimeStamp(); | ||
|
||
std::cout << std::dec; | ||
|
||
std::cout << "Time: " << (end - start) / 1e9 << " sec" << std::endl; | ||
std::cout << "Throughput: " << N / ((end - start) / 1e9) << " ops/s" << std::endl; | ||
std::cout << "Time per operation: " << (end - start) / N << " ns" << std::endl; | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
#include "UuidGenerator.h" | ||
|
||
UuidGenerator::UuidGenerator() | ||
{ | ||
std::random_device random_device; | ||
|
||
for (size_t i = 0; i < 2; ++i) | ||
{ | ||
std::seed_seq seed{random_device(), random_device()}; | ||
m_Engines[i].seed(seed); | ||
} | ||
} | ||
|
||
void UuidGenerator::GenerateUuid(char *uuid) | ||
{ | ||
for (size_t i = 0; i < 2; ++i) | ||
{ | ||
std::uint64_t value = m_Engines[i](); | ||
*reinterpret_cast<std::uint64_t *>(uuid + i * 8) = value; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
#ifndef UUID_GENERATOR_H | ||
#define UUID_GENERATOR_H | ||
|
||
#include <random> | ||
|
||
class UuidGenerator | ||
{ | ||
public: | ||
UuidGenerator(); | ||
|
||
void GenerateUuid(char *uuid); | ||
|
||
private: | ||
std::mt19937_64 m_Engines[2]; | ||
}; | ||
|
||
#endif // UUID_GENERATOR_H |