bmaxa@maxa:~/haskell$ time ./createMatrixDump.py -N 128 > output.txt
real 0m0.041s
user 0m0.040s
sys 0m0.000s
bmaxa@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.081s
user 0m0.072s
sys 0m0.008s
bmaxa@maxa:~/haskell$ time ./printMatrixDecay < 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.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.018s
user 0m0.012s
sys 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@gmail.com
Date: Fri, 8 Feb 2013 12:26:09 -0700
To: haskell-cafe@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 | ./printMatrixDecay
real 0m2.655s
user 0m2.677s
sys 0m0.095s
$ time ./createMatrixDump.py -N 128 | ./printMatrixDecay.py -
real 0m0.445s
user 0m0.615s
sys 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@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe