[Haskell-cafe] performance question

Branimir Maksimovic bmaxa at hotmail.com
Sat Feb 9 00:57:42 CET 2013


Heh, I have wrote c++ version and that is much faster than python ;) bmaxa at maxa:~/haskell$ time ./createMatrixDump.py -N 128 > output.txt
real    0m0.041suser    0m0.040ssys     0m0.000sbmaxa at maxa:~/haskell$ time ./printMatrixDecay.py - < output.txt(-) read 16384 matrix elements (128x128 = 16384)[0.00e+00, 1.00e-08) = 0 (0.00%) 0[1.00e-08, 1.00e-07) = 0 (0.00%) 0[1.00e-07, 1.00e-06) = 0 (0.00%) 0[1.00e-06, 1.00e-05) = 0 (0.00%) 0[1.00e-05, 1.00e-04) = 1 (0.00%) 1[1.00e-04, 1.00e-03) = 15 (0.00%) 16[1.00e-03, 1.00e-02) = 149 (0.00%) 165[1.00e-02, 1.00e-01) = 1425 (0.00%) 1590[1.00e-01, 1.00e+00) = 14794 (0.00%) 16384[1.00e+00, 2.00e+00) = 0 (0.00%) 16384
real    0m0.081suser    0m0.072ssys     0m0.008sbmaxa at maxa:~/haskell$ time ./printMatrixDecay < output.txtread 16384 matrix elements (128x128 = 16384)[0.00e+00, 1.00e-08) = 0 (0.00%) 0[1.00e-08, 1.00e-07) = 0 (0.00%) 0[1.00e-07, 1.00e-06) = 0 (0.00%) 0[1.00e-06, 1.00e-05) = 0 (0.00%) 0[1.00e-05, 1.00e-04) = 1 (0.01%) 1[1.00e-04, 1.00e-03) = 15 (0.09%) 16[1.00e-03, 1.00e-02) = 149 (0.91%) 165[1.00e-02, 1.00e-01) = 1425 (8.70%) 1590[1.00e-01, 1.00e+00) = 14794 (90.30%) 16384[1.00e+00, 2.00e+00) = 0 (0.00%) 16384
real    0m0.018suser    0m0.012ssys     0m0.004s
unfortunately g++ does not have regex implemented yet so I used libpcre ...
#include <pcre.h>#include <sstream>#include <cstdio>#include <cmath>#include <iostream>#include <stdexcept>#include <vector>
template <class F>void regex(const std::string& in, const std::string& pattern,int n,F f){    int ovec[3*n],position;    const char* error;       int errorpos;
    pcre* pe = pcre_compile(pattern.c_str(),0,&error,&errorpos,0);    if(!pe)throw std::runtime_error(error);
    pcre_extra* extra=pcre_study(pe,0,&error);
    for(position = 0;        pcre_exec(pe,extra,in.c_str(),in.size(),position,0,ovec,3*n)>=0;        position = ovec[1])f(position,ovec);    f(position,ovec);    pcre_free(extra);    pcre_free(pe);   }
int main(){  std::ios::sync_with_stdio(false);  std::ostringstream oss;  oss << std::cin.rdbuf();  const std::string& in = oss.str();  std::vector<double> strataBounds = { 0.0, 1.0e-8, 1.0e-7, 1.0e-6, 1.0e-5, 1.0e-4, 1.0e-3, 1.0e-2, 1.0e-1, 1.0, 2.0 };  std::vector<int> strataCounts(strataBounds.size());  unsigned N = 0;  auto f = [&](int position,int* ovec)  {    if(int(position) > ovec[0])return;        ++N;    double aij = 0.0;    std::istringstream iss(in.substr(ovec[2],ovec[3]-ovec[2]));    iss >> aij;    aij=fabs(aij);    for(unsigned i = 0; i < strataBounds.size() - 1; ++i)    {      if(aij >= strataBounds[i] && aij < strataBounds[i+1])      {        ++strataCounts[i];        break;      }    }  };  regex(in,"matrix.*= ([0-9.eE+-]+)\n",2,f);  printf("read %d matrix elements (%dx%d = %d)\n",N,int(sqrt(N)),int(sqrt(N)),N);  int total = 0;  for(unsigned i = 0; i< strataBounds.size()-1;++i)  {    total += strataCounts[i];    printf("[%1.2e, %1.2e) = %d (%1.2f%%) %d\n", strataBounds[i], strataBounds[i+1],    strataCounts[i], 100*(double(strataCounts[i])/N), total);  }}


From: nicolasbock at gmail.com
Date: Fri, 8 Feb 2013 12:26:09 -0700
To: haskell-cafe at haskell.org
Subject: [Haskell-cafe] performance question

Hi list,
I wrote a script that reads matrix elements from standard input, parses the input using a regular expression, and then bins the matrix elements by magnitude. I wrote the same script in python (just to be sure :) ) and find that the python version vastly outperforms the Haskell script.


To be concrete:
$ time ./createMatrixDump.py -N 128 | ./printMatrixDecayreal    0m2.655s
user    0m2.677ssys     0m0.095s


$ time ./createMatrixDump.py -N 128 | ./printMatrixDecay.py -
real    0m0.445suser    0m0.615ssys     0m0.032s
The Haskell script was compiled with "ghc --make printMatrixDecay.hs".


Could you have a look at the script and give me some pointers as to where I could improve it, both in terms of performance and also generally, as I am very new to Haskell.


Thanks already,
nick


_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe at haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe 		 	   		  
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/haskell-cafe/attachments/20130208/0402b531/attachment.htm>


More information about the Haskell-Cafe mailing list