Semergence

Seth Ladd’s blog about Ruby on Rails and crunching data.

Tail Recursive Line Processing in Erlang

with one comment

When I was testing performance in Erlang using funs, I mentioned I wanted to see what would happen if I made the functions tail recursive. I just took a crack at it, and it looks like making this particular function tail recursive didn’t help performance. (as always, please let me know if my assumptions are incorrect.)

Tail Recursive Line Processing


process_file(Filename) ->
	{ok, File} = file :o pen(Filename, read),
	process_lines(File, first, 0).

process_line(eof) -> done;
process_line(Line) ->
	L = string:tokens(string:strip(Line, both, $\n), "	"),
	{Dimensions, Measures} = lists:split(10, L),
	lists:map(fun(X) -> {I,_} = string:to_integer(X), I end, Measures).

process_lines(_, eof, _) -> done;
process_lines(File, PreviousLine, LineNum) ->
	Line = io:get_line(File, ''),
	process_line(Line),
	process_lines(File, Line, LineNum + 1).

Running this code agains a file of 10037355 lines and 1028071833 bytes big, it takes on average 401.40 seconds. This is only slightly better than the previous code attempts that are not tail recursive (and imo easier to read).

Written by sethladd

September 12, 2007 at 10:47 pm

Posted in erlang

One Response to 'Tail Recursive Line Processing in Erlang'

Subscribe to comments with RSS or TrackBack to 'Tail Recursive Line Processing in Erlang'.

  1. yeah. the point of tail recursion is to not grow the stack one level for each iteration.. nothing to do with ‘performance’ until you hit the brick wall of stack overflow :)

    carmen

    13 Sep 07 at 10:29 am

Leave a Reply