Created
February 1, 2019 09:05
-
-
Save marty1885/d49602f54ae24f1fb421bd7803a51282 to your computer and use it in GitHub Desktop.
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
//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(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example output:
(On I5 8250U, Linux, GCC 8.2.1)