3 ms·
Your OCaml code is not doing the same thing as the Python one: you are parsing a regexp in a loop, and then splitting by the regexp - not by a delimiter. I mov
by leovonl 11y ago
Your OCaml code is not doing the same thing as the Python one: you are parsing a regexp in a loop, and then splitting by the regexp - not by a delimiter.
I moved the regexp instantiation outside the loop, see the results below ("orig" is your code, "proper" has the regexp parsed only once):
> bash -c 'time ./orig bigdata.csv'
15502592020.,15502065537.6,15519223046.6,15498884970.,15502078298.,15519530367.3,15510803256.2,15519590717.2,15511590976.
real 0m3.110s
user 0m3.107s
sys 0m0.003s
> bash -c 'time ./proper bigdata.csv'
15502592020.,15502065537.6,15519223046.6,15498884970.,15502078298.,15519530367.3,15510803256.2,15519590717.2,15511590976.
real 0m2.596s
user 0m2.590s
sys 0m0.003s
I'd say the rest of the difference is probably due to the standard library performance itself. BTW, compilation time for ocamlopt is 70ms.
- cannam 11y agoAny idea why the compiler doesn't hoist the constant single-character regexp automatically?
- leovonl 11y ago1) It's not a constant expression, it's a function call. 2) The compiler does not know if there are collateral effects or not. 3) Ocaml is strict eval - no memoisation Therefore this would be an unsafe optimisation if the compiler tried to perform it, which could lead to unintended behavioural changes. The only way to fix this properly would be to add an effects system in some sense, at least a way to mark pure/impure functions.