File with list of lists of integers

12 views
Skip to first unread message

Gilmara Pompelli

unread,
Oct 15, 2010, 4:57:13 PM10/15/10
to Erlang Programming
I have a file such:

lista.txt
[[22,4,55,7],[77,3,66,23],[44,56,23,90]]

I need to do a program to read this file so I can use the list as a
variable of type [[Int]], to then apply a sort.

Can anyone help me?

Thanks
Gilmara

Andrey

unread,
Oct 15, 2010, 10:33:58 PM10/15/10
to Erlang Programming
If you put a period at the end of the line in your file:

[[22,4,55,7],[77,3,66,23],[44,56,23,90]].

you can read the file with file:consult/1 function:

1> {ok, [Data]} = file:consult("lista.txt").
{ok,[[[22,4,55,7],[77,3,66,23],[44,56,23,90]]]}
2> Data.
[[22,4,55,7],[77,3,66,23],[44,56,23,90]]


On Oct 15, 4:57 pm, Gilmara Pompelli <gilmarampompe...@gmail.com>
wrote:

Gilmara Pompelli

unread,
Oct 16, 2010, 12:30:57 PM10/16/10
to erlang-prog...@googlegroups.com
Thanks

2010/10/15 Andrey <andrey.p...@gmail.com>
--
Erlang Programming Website:
http://www.erlangprogramming.org/



--
Tenha um melhor amigo.Preserve seu direito a vida. Isso é lei.

Art. 2º - Aquele que, em lugar público ou privado, aplicar ou fizer aplicar maus tratos aos animais, incorrerá em multa de Cr$.. e na pena de prisão celular de 2 a 15 dias, quer o delinqüente seja ou não o respectivo proprietário, sem prejuízo da ação civil que possa caber.
Decreto Lei nº 24645/34.

Visite:

www.ufpe.br/adoteumviralata

Gilmara Pompelli

unread,
Oct 18, 2010, 2:31:41 PM10/18/10
to erlang-prog...@googlegroups.com
Hello

I have a file mkList.txt (but my mkList, have 100 lists with 100 numbers)


 [[22,4,55,7],[77,3,66,23],[44,56,23,90]]

And

I need to know the time that Erlang uses to read the file list using
map/sort and pmap/sort. I did this:

-module(teste).
-export([teste/1]).
-import(lists, [map/2]).
-import(lib_misc, [pmap/2]).

teste(1) ->
  {ok, [Data]} = file:consult("mkList1.txt"),

  {Time1, T} = timer:tc(lists, map, [fun lists:sort/1,Data]),

  {Time2, R} = timer:tc(lib_misc, pmap, [fun lists:sort/1,Data]),

 {Time1, T, Time2, R}.

The question is what is wrong in my code, it seems to me that is not
calculating the time correctly.

Time1 = 15000
Time 2 = 1.

Could anyone help me?
Thanks.

Em 16 de outubro de 2010 13:30, Gilmara Pompelli
<gilmaram...@gmail.com> escreveu:

Reza Moussavi

unread,
Oct 18, 2010, 3:32:50 PM10/18/10
to Erlang Programming
Hi,
The result of timer:tc function is in microseconds (1/1000000 sec.) so
when you get 1microsec as result it means function has not finished
and some error has happen. to check out what has happen mention T,R in
your result that one of them should be started by {'EXIT',...


On Oct 18, 8:31 pm, Gilmara Pompelli <gilmarampompe...@gmail.com>
wrote:
> Hello
>
> I have a file mkList.txt (but my mkList, have 100 lists with 100 numbers)
>  [[22,4,55,7],[77,3,66,23],[44,56,23,90]]
>
> And
>
> I need to know the time that Erlang uses to read the file list using
> map/sort and pmap/sort. I did this:
>
> -module(teste).
> -export([teste/1]).
> -import(lists, [map/2]).
> -import(lib_misc, [pmap/2]).
>
> teste(1) ->
>   {ok, [Data]} = file:consult("mkList1.txt"),
>
>   {Time1, T} = timer:tc(lists, map, [fun lists:sort/1,Data]),
>
>   {Time2, R} = timer:tc(lib_misc, pmap, [fun lists:sort/1,Data]),
>
>  {Time1, T, Time2, R}.
>
> The question is what is wrong in my code, it seems to me that is not
> calculating the time correctly.
>
> Time1 = 15000
> Time 2 = 1.
>
> Could anyone help me?
> Thanks.
>
> Em 16 de outubro de 2010 13:30, Gilmara Pompelli
> <gilmarampompe...@gmail.com> escreveu:
>
>
>
>
>
>
>
> > Thanks
>
> > 2010/10/15 Andrey <andrey.paramo...@gmail.com>

Reza Moussavi

unread,
Oct 18, 2010, 4:08:29 PM10/18/10
to Erlang Programming
You can try following code and run teste:run_both(1000) then the time
would be based on running them 1000 times to have better estimation on
time result.

-module(teste).
-export([teste/1,pmap/2,run_map/2,run_pmap/2,load_data/0,run_both/1]).
-import(lists, [map/2]).


pmap(F, L) ->
Parent=self(),
Pids=[spawn(fun()->Parent!{self(),F(X)} end) || X<-L],
[receive {Pid,Res}->Res end || Pid<-Pids].

load_data()->
{ok, [Data]} = file:consult("mkList1.txt"),
Data.

run_map(_Data,0)->map_ok;
run_map(Data,N)->
lists:map(fun lists:sort/1,Data),
run_map(Data,N-1).

run_pmap(_Data,0)->pmap_ok;
run_pmap(Data,N)->
pmap(fun lists:sort/1,Data),
run_pmap(Data,N-1).

run_both(N)->
Data=load_data(),
{T1,_R1}=timer:tc(?MODULE,run_pmap,[Data,N]),
{T2,_R2}=timer:tc(?MODULE,run_map,[Data,N]),
[{N,times_ran},{pmap_time,T1},{map_time,T2}].

Gilmara Pompelli

unread,
Oct 18, 2010, 4:12:43 PM10/18/10
to erlang-prog...@googlegroups.com
I do not understand, how can I know if this happened? In my test did
not show error messages.

2010/10/18 Reza Moussavi <reza.m...@gmail.com>:

Reza Moussavi

unread,
Oct 18, 2010, 6:29:53 PM10/18/10
to Erlang Programming
try your program with 2 list of 2 elements : put [[10,20],[30,2]]. in
your txt file and run the program you wrote up here then copy paste
the result here, I will check again.

On Oct 18, 10:12 pm, Gilmara Pompelli <gilmarampompe...@gmail.com>
wrote:
> I do not understand, how can I know if this happened? In my test did
> not show error messages.
>
> 2010/10/18 Reza Moussavi <reza.muss...@gmail.com>:

Gilmara Pompelli

unread,
Oct 21, 2010, 10:40:23 AM10/21/10
to erlang-prog...@googlegroups.com
Hello, Reza

I did what you said, ran the program with two lists and two elements,
the result is here.

20> teste:teste(1).
{1,[[10,20],[2,30]],1,[[10,20],[2,30]]}

Thanks

2010/10/18 Reza Moussavi <reza.m...@gmail.com>:

Reza Moussavi

unread,
Oct 21, 2010, 6:07:11 PM10/21/10
to Erlang Programming
If you have small lists it take faster to do with map because pmap
takes longer to spawn and send and receive rather that sorting small
amount of numbers so I tried 100 lists each contains 1000 integers and
i test them by my program that I had wrote above
the result says pmap:47milisec and map:70milisec (approximately)

still I dont know why sometimes on samll lists some timer:tc returns
1, but on large amount of data it never return 1 on my computer.
the results I got on a 316KB text file including 100 lists of 1000
integers shown below:

74> teste:run_both(1).
[{1,times_ran},{pmap_time,47000},{map_time,77999}]
75> teste:run_both(1).
[{1,times_ran},{pmap_time,47000},{map_time,61999}]
76> teste:run_both(1).
[{1,times_ran},{pmap_time,47000},{map_time,62999}]
77> teste:run_both(1).
[{1,times_ran},{pmap_time,47000},{map_time,61999}]
78> teste:run_both(1).
[{1,times_ran},{pmap_time,47000},{map_time,77999}]
79> teste:run_both(1).
[{1,times_ran},{pmap_time,46000},{map_time,78999}]
80> teste:run_both(1).
[{1,times_ran},{pmap_time,62000},{map_time,62999}]
81> teste:run_both(1).
[{1,times_ran},{pmap_time,47000},{map_time,62999}]
82> teste:run_both(1).
[{1,times_ran},{pmap_time,32000},{map_time,77999}]
83> teste:run_both(1).
[{1,times_ran},{pmap_time,79000},{map_time,155999}]
84> teste:run_both(1).
[{1,times_ran},{pmap_time,31000},{map_time,78999}]
85> teste:run_both(1).
[{1,times_ran},{pmap_time,47000},{map_time,62999}]

good luck!

On Oct 21, 4:40 pm, Gilmara Pompelli <gilmarampompe...@gmail.com>
wrote:
> Hello, Reza
>
> I did what you said, ran the program with two lists and two elements,
> the result is here.
>
> 20> teste:teste(1).
> {1,[[10,20],[2,30]],1,[[10,20],[2,30]]}
>
> Thanks
>
> 2010/10/18 Reza Moussavi <reza.muss...@gmail.com>:

Gilmara Pompelli

unread,
Oct 22, 2010, 8:06:43 AM10/22/10
to erlang-prog...@googlegroups.com
Thank you for your attention, I'm learning Erlang, alone, so sometimes
I get many questions, I thought the program was in error. Thanks.

2010/10/21 Reza Moussavi <reza.m...@gmail.com>:

Gilmara Pompelli

unread,
Oct 22, 2010, 8:47:07 AM10/22/10
to erlang-prog...@googlegroups.com
I did some testing and it seems that Erlang is faster to read the file
ready after the map / sort than to make a list and then the map /
sort. Strange is not it?

2010/10/21 Reza Moussavi <reza.m...@gmail.com>:

Gilmara Pompelli

unread,
Nov 5, 2010, 10:42:23 AM11/5/10
to erlang-prog...@googlegroups.com
Which format should I use to convert microseconds into seconds?

{Sort, Time1/1000000}

Return
1.0e-6


Thanks


2010/10/22 Gilmara Pompelli <gilmaram...@gmail.com>:

Reply all
Reply to author
Forward
0 new messages