Skip to content

Instantly share code, notes, and snippets.

@hiragram
Created July 15, 2014 15:02
Show Gist options
  • Select an option

  • Save hiragram/3370a92c66561de5be2b to your computer and use it in GitHub Desktop.

Select an option

Save hiragram/3370a92c66561de5be2b to your computer and use it in GitHub Desktop.
MySQLのDateTimeの形のstringをC++でソートするときタイムスタンプにするのとハイフンコロン取っ払うのとでどっちが速いのか → タイムスタンプ1.7秒、正規表現15秒
#include <iostream>
#include <string>
#include <regex>
using namespace std;
long long int getIntValueFromDateString(string calendar_date);
int main()
{
string calendar_date = "2014-04-01 06:00:00";
for(int i = 0 ; i < 1000000 ; i++)
{
getIntValueFromDateString(calendar_date);
}
}
long long int getIntValueFromDateString(string calendar_date)
{
regex pattern("\\D");
string result = regex_replace(calendar_date , pattern , "");
return stoll(result);
}
#include <iostream>
#include <string>
using namespace std;
int getTimestampFromDateString(string calendar_date);
int main()
{
string calendar_date = "2014-04-01 06:00:00";
for(int i = 0 ; i < 1000000 ; i++)
{
getTimestampFromDateString(calendar_date);
}
}
int getTimestampFromDateString(string calendar_date)
{
struct tm tmlol;
strptime(calendar_date.c_str() , "%Y-%m-%d %H:%M:%S" , &tmlol);
time_t t = mktime(&tmlol);
return t;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment