[erlang-questions] timing ++ operation

Wes James comptekki@REDACTED
Sat Apr 9 04:55:15 CEST 2011


I have this to produce some data:


-module(rep).

-export([s/2]).

s(Str, Cnt) when (Cnt == 0) -> [Str++"0"];
s(Str, Cnt) ->
	[Str++integer_to_list(Cnt)|s(Str, Cnt-1)].


It is run like this:

rep:s("a",10).
["a10","a9","a8","a7","a6","a5","a4","a3","a2","a1","a0"]

I then run this to create a test erl file which has a 10000 long
A="a1" ++ "a2" ..... "a10000":

-module(wf).

-export([s/0]).

s() ->
	FileName = "plus.erl",
	{ok, Stream} = file:open(FileName, [write]),  %append
	
	io:format(Stream, "-module(plus).
-export([s/0]).

s() ->

statistics(runtime),
statistics(wall_clock),
A=\"0\" ", []),

	R=rep:s("a",10000),
	[io:format(Stream, " ++ ~p", [X])|| X <- R],
	
	io:format(Stream, ",
{_, Time1} = statistics(runtime),
{_, Time2} = statistics(wall_clock),
U1 = Time1 * 1000,
U2 = Time2 * 1000,
io:format(\"Process time=~~p (~~p) microseconds~~n\", [U1, U2]).
", []),

	file:close(Stream).

I then c(plus) and then plus:s(). and the process time and
microseconds are 0.  Is that because the string of ++ items is compile
in to one big string and all the operation does is assign it to A?

-wes



More information about the erlang-questions mailing list