Skip to content

Instantly share code, notes, and snippets.

@Hperigo
Created October 9, 2017 13:52
Show Gist options
  • Save Hperigo/d991b64a663cb796825377b02047f2ce to your computer and use it in GitHub Desktop.
Save Hperigo/d991b64a663cb796825377b02047f2ce to your computer and use it in GitHub Desktop.
split string test
#include "cinder/app/App.h"
#include "cinder/app/RendererGl.h"
#include "cinder/gl/gl.h"
#include "cinder/Timer.h"
#include "cinder/Utilities.h"
using namespace ci;
using namespace ci::app;
using namespace std;
std::vector<std::string> customSplit(std::string str, const std::string& separators, bool compress = true){
std::vector<std::string> result;
std::size_t searchPrevPos = 0, searchPos;
while ((searchPos = str.find_first_of(separators, searchPrevPos)) != std::string::npos)
{
if ( searchPos >= searchPrevPos && ! compress ){
result.push_back( str.substr( searchPrevPos, searchPos - searchPrevPos ));
}
else if( searchPos > searchPrevPos ){
result.push_back( str.substr( searchPrevPos, searchPos - searchPrevPos ));
}
searchPrevPos = searchPos+1;
}
if (searchPrevPos <= str.length())
result.push_back(str.substr(searchPrevPos, std::string::npos));
return result;
}
class splitStringApp : public App {
public:
void setup() override;
void mouseDown( MouseEvent event ) override;
void update() override;
void draw() override;
};
void splitStringApp::setup()
{
std::string s = "Cinder,OpenFrameworks-- ThreeJs, Processing-";
std::string delimiter = ",- ";
int numOfTests = 1;
bool compress = false;
ci::Timer t;
t.start();
std::vector<std::string> output;
for(int i = 0; i < numOfTests; i++){
output = customSplit(s, delimiter, compress);
}
t.stop();
console() << "Custom impl took: " << t.getSeconds() << std::endl;
int index = 0;
for(auto& s : output){
console() << index << ". '" << s << "' "<<endl;
index ++;
}
console() << "--------\n\n";
t.start();
std::vector<std::string> outputCi;
for(int i = 0; i < numOfTests; i++){
outputCi = ci::split(s, delimiter, compress);
}
t.stop();
console() << "boost took: " << t.getSeconds() << std::endl;
index = 0;
for(auto& s : outputCi){
console() << index << ". '" << s << "' "<<endl;
index ++;
}
}
void splitStringApp::mouseDown( MouseEvent event )
{
}
void splitStringApp::update()
{
}
void splitStringApp::draw()
{
gl::clear( Color( 0, 0, 0 ) );
}
CINDER_APP( splitStringApp, RendererGl )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment