Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
//build: c++ tmperf.cpp -o tmperf -O3 -lnupic_core
//usage: ./tmperf
#include <nupic/algorithms/TemporalMemory.hpp>
#include <chrono>
#include <vector>
#include <random>
using TemporalMemory = nupic::algorithms::temporal_memory::TemporalMemory; //Yeah.... This is too redundent
const int TM_INPUT_SIZE = 64;
const float INPUT_DENSITY = 0.1f;
const size_t NUM_ITER = 40000;
const size_t ITER_REPORT = 1000;
int main()
{
//To generate random SDR efficently
std::vector<UInt> all_columns(TM_INPUT_SIZE);
for(size_t i=0;i<TM_INPUT_SIZE;i++)
all_columns[i] = i;
auto randomSDR = [&all_columns](auto& res){
static std::mt19937 rng;
std::shuffle(all_columns.begin(), all_columns.end(), rng);
std::copy(all_columns.begin(), all_columns.begin()+res.size(), res.begin());
std::sort(res.begin(), res.end());
};
std::cout << "TemporalMemory performance test. Parameters:\n"
<< "Input SDR size: " << TM_INPUT_SIZE << '\n'
<< "SDR Density: " << INPUT_DENSITY << '\n';
//The main code
std::vector<UInt> active_columns(TM_INPUT_SIZE*INPUT_DENSITY);
TemporalMemory tm({TM_INPUT_SIZE});
auto t1 = std::chrono::high_resolution_clock::now();
for(size_t i=0;i<NUM_ITER;i++) {
randomSDR(active_columns);
tm.compute(active_columns.size(), active_columns.data(), true);
if((i+1)%ITER_REPORT == 0) {
auto t2 = std::chrono::high_resolution_clock::now();
std::cout << "Iteration " << i+1 << ": time per TM execution is "
<< std::chrono::duration_cast<std::chrono::duration<double>>(t2-t1).count()/ITER_REPORT*1000 << " ms." << std::endl; //Force flush
t1 = std::chrono::high_resolution_clock::now();
}
}
}
@marty1885

This comment has been minimized.

Copy link
Owner Author

commented Feb 1, 2019

Example output:
(On I5 8250U, Linux, GCC 8.2.1)

Input SDR size: 64
SDR Density: 0.1
Iteration 1000: time per TM execution is 0.0727128 ms.
Iteration 2000: time per TM execution is 0.0772516 ms.
Iteration 3000: time per TM execution is 0.10591 ms.
Iteration 4000: time per TM execution is 0.141974 ms.
Iteration 5000: time per TM execution is 0.171026 ms.
Iteration 6000: time per TM execution is 0.213778 ms.
Iteration 7000: time per TM execution is 0.256422 ms.
Iteration 8000: time per TM execution is 0.310993 ms.
Iteration 9000: time per TM execution is 0.355887 ms.
Iteration 10000: time per TM execution is 0.424861 ms.
Iteration 11000: time per TM execution is 0.522072 ms.
Iteration 12000: time per TM execution is 0.552158 ms.
Iteration 13000: time per TM execution is 0.609994 ms.
Iteration 14000: time per TM execution is 0.679534 ms.
Iteration 15000: time per TM execution is 0.848373 ms.
Iteration 16000: time per TM execution is 1.06723 ms.
Iteration 17000: time per TM execution is 1.25357 ms.
Iteration 18000: time per TM execution is 1.45957 ms.
Iteration 19000: time per TM execution is 1.56519 ms.
Iteration 20000: time per TM execution is 1.67367 ms.
Iteration 21000: time per TM execution is 1.7592 ms.
Iteration 22000: time per TM execution is 1.84701 ms.
Iteration 23000: time per TM execution is 1.90377 ms.
Iteration 24000: time per TM execution is 2.08463 ms.
Iteration 25000: time per TM execution is 2.25694 ms.
Iteration 26000: time per TM execution is 2.44186 ms.
Iteration 27000: time per TM execution is 2.63037 ms.
Iteration 28000: time per TM execution is 2.82431 ms.
Iteration 29000: time per TM execution is 3.01219 ms.
Iteration 30000: time per TM execution is 3.64744 ms.
Iteration 31000: time per TM execution is 3.87887 ms.
Iteration 32000: time per TM execution is 4.08649 ms.
Iteration 33000: time per TM execution is 4.28914 ms.
Iteration 34000: time per TM execution is 4.60198 ms.
Iteration 35000: time per TM execution is 5.42536 ms.
Iteration 36000: time per TM execution is 5.63289 ms.
Iteration 37000: time per TM execution is 5.00891 ms.
Iteration 38000: time per TM execution is 5.20788 ms.
Iteration 39000: time per TM execution is 5.39356 ms.
Iteration 40000: time per TM execution is 5.61194 ms.
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.