-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #676 from leapmotion/feature-bmobjpool
Add a benchmark test for the object pool
- Loading branch information
Showing
4 changed files
with
78 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
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,65 @@ | ||
// Copyright (C) 2012-2015 Leap Motion, Inc. All rights reserved. | ||
#include "stdafx.h" | ||
#include "ObjectPoolBm.h" | ||
#include "Benchmark.h" | ||
#include <autowiring/ObjectPool.h> | ||
#include <functional> | ||
#include <vector> | ||
|
||
static const size_t n = 100; | ||
|
||
// Dummy struct of ten integers--needed because unique_ptr<int[10]> doesn't compile | ||
struct tenPack { int dummy[10]; }; | ||
|
||
// Type that will perform a secondary allocation on construction | ||
struct filling_vector { | ||
filling_vector(void) { | ||
other.resize(100); | ||
} | ||
|
||
std::vector<int> other; | ||
}; | ||
|
||
template<typename T> | ||
void profile_basic(Stopwatch& sw) { | ||
std::vector<std::unique_ptr<T>> mem; | ||
|
||
// Initial set of allocations: | ||
for (size_t i = n; i--;) | ||
mem.emplace_back(new T); | ||
mem.clear(); | ||
|
||
// Now simulate something where an object pool might have been handy | ||
for (size_t k = n; k--;) { | ||
sw.Start(); | ||
for (size_t i = n; i--;) | ||
mem.emplace_back(new T); | ||
sw.Stop(n * n); | ||
mem.clear(); | ||
} | ||
} | ||
|
||
template<typename T> | ||
void profile_pool(Stopwatch& sw) { | ||
ObjectPool<T> pool; | ||
pool.Preallocate(n); | ||
|
||
// Now simulate something where an object pool might have been handy | ||
std::vector<std::shared_ptr<T>> mem; | ||
for (size_t k = n; k--;) { | ||
sw.Start(); | ||
for (size_t i = n; i--;) | ||
mem.emplace_back(pool()); | ||
sw.Stop(n * n); | ||
mem.clear(); | ||
} | ||
} | ||
|
||
Benchmark ObjectPoolBm::Allocation(void) { | ||
return { | ||
{ "basic", &profile_basic<tenPack> }, | ||
{ "complex", &profile_basic<filling_vector> }, | ||
{ "pool_basic", &profile_pool<tenPack> }, | ||
{ "pool_complex", &profile_pool<filling_vector> } | ||
}; | ||
} |
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,9 @@ | ||
// Copyright (C) 2012-2015 Leap Motion, Inc. All rights reserved. | ||
#pragma once | ||
|
||
struct Benchmark; | ||
|
||
class ObjectPoolBm { | ||
public: | ||
static Benchmark Allocation(void); | ||
}; |