Re: [Pharo-dev] Freeze while running a loop

2015-04-04 Thread Gaurav Singh
Thanks Alain :)

On Sat, Apr 4, 2015 at 5:02 PM, Alain Rastoul  wrote:

> Hi Gaurav,
>
> As said by Aliaksei, the problem is string concatenation with ,
> your example takes 33 secs on a core i7 3.3Ghz (quite some ms ;) )
> In Smalltalk, you handle that with streams (see Aliaksei example).
> You can also add objects to all types of collections, not only strings
> (Arrays etc).
> You will find examples by searching references of WriteStream class (in
> Nautilus, on WriteStream, right click / Analyse Class refs) or senders of
> streamContents: .
> Have a look at Pharo by examples chapter on streams
> http://pharo.gforge.inria.fr/PBE1/PBE1ch11.html
> (pdf here: http://pharobyexample.org/)
>
> And if you want to see what is going on with some code, you can select
> your code in the workspace and use the 'Profile it' option in the menu.
> Alternatively, you can open the profiler tools with World Menu/Tools/Time
> Profiler, paste your code in the code pane of the profiler and run it.
> A very handy tool, worth to know about it.
>
> HTH
>
> Regards,
>
> Alain
>
>
>


Re: [Pharo-dev] Freeze while running a loop

2015-04-04 Thread Alain Rastoul

Hi Gaurav,

As said by Aliaksei, the problem is string concatenation with ,
your example takes 33 secs on a core i7 3.3Ghz (quite some ms ;) )
In Smalltalk, you handle that with streams (see Aliaksei example).
You can also add objects to all types of collections, not only strings 
(Arrays etc).
You will find examples by searching references of WriteStream class (in 
Nautilus, on WriteStream, right click / Analyse Class refs) or senders 
of streamContents: .

Have a look at Pharo by examples chapter on streams
http://pharo.gforge.inria.fr/PBE1/PBE1ch11.html
(pdf here: http://pharobyexample.org/)

And if you want to see what is going on with some code, you can select 
your code in the workspace and use the 'Profile it' option in the menu.
Alternatively, you can open the profiler tools with World 
Menu/Tools/Time Profiler, paste your code in the code pane of the 
profiler and run it.

A very handy tool, worth to know about it.

HTH

Regards,

Alain




Re: [Pharo-dev] Freeze while running a loop

2015-04-03 Thread Jigyasa Grover
Yep , quite possible.

On Fri, Apr 3, 2015 at 5:37 PM, Max Leske  wrote:

>
> On 03 Apr 2015, at 14:05, Jigyasa Grover 
> wrote:
>
> It didn't happen with me in either of the cases :/
> Worked just fine for both.
>
>
> May be memory / hardware dependent.
>
>
> On Fri, Apr 3, 2015 at 5:33 PM, Gaurav Singh 
> wrote:
>
>> It doesnt happen here , but i was wondering why did it happen in the
>> string case even when the run time is just a few milliseconds ?
>>
>
>
>


Re: [Pharo-dev] Freeze while running a loop

2015-04-03 Thread Max Leske

> On 03 Apr 2015, at 14:05, Jigyasa Grover  wrote:
> 
> It didn't happen with me in either of the cases :/
> Worked just fine for both.

May be memory / hardware dependent.

> 
> On Fri, Apr 3, 2015 at 5:33 PM, Gaurav Singh  > wrote:
> It doesnt happen here , but i was wondering why did it happen in the string 
> case even when the run time is just a few milliseconds ?
> 



Re: [Pharo-dev] Freeze while running a loop

2015-04-03 Thread Jigyasa Grover
It didn't happen with me in either of the cases :/
Worked just fine for both.

On Fri, Apr 3, 2015 at 5:33 PM, Gaurav Singh  wrote:

> It doesnt happen here , but i was wondering why did it happen in the
> string case even when the run time is just a few milliseconds ?
>


Re: [Pharo-dev] Freeze while running a loop

2015-04-03 Thread Gaurav Singh
It doesnt happen here , but i was wondering why did it happen in the string
case even when the run time is just a few milliseconds ?


Re: [Pharo-dev] Freeze while running a loop

2015-04-03 Thread Gaurav Singh
Thanks Aliaksei , I got it :)

On Fri, Apr 3, 2015 at 5:33 PM, Gaurav Singh  wrote:

> It doesnt happen here , but i was wondering why did it happen in the
> string case even when the run time is just a few milliseconds ?
>


Re: [Pharo-dev] Freeze while running a loop

2015-04-03 Thread Aliaksei Syrel
Hi Gaurav,

The problem is with your code. String is immutable and during concatenation
it creates each time new and new String objects. In the end the length of
the "temp" string is 588895. What do you think, is it efficient to create
thousands of Strings with half a million length just to add a few
characters at the end?

You have to use a stream to concatenate so many strings:

| stream |
> stream := ReadWriteStream on: String new.
> (1 to: 10)
> do: [:i | stream << (i asString, ' ')].
> stream contents "returns your temp string"


Cheers,
Alex

On Fri, Apr 3, 2015 at 1:43 PM, Gaurav Singh  wrote:

> My window freezes whenever i try to run a loop which has even 10
> iterations which are quite less for a computational device.
> Let say i run :
> [| temp |
> temp := String new.
> (1 to: 10)
> do: [:i | temp := temp, i asString, ' ']] timeToRun.
> The time taken to compute is a few milliseconds , but the program freezes
> for quite a few seconds, What can be the problem ?
>


Re: [Pharo-dev] Freeze while running a loop

2015-04-03 Thread Max Leske

> On 03 Apr 2015, at 13:43, Gaurav Singh  wrote:
> 
> My window freezes whenever i try to run a loop which has even 10 
> iterations which are quite less for a computational device.
> Let say i run : 
> [| temp |
> temp := String new.
> (1 to: 10)
> do: [:i | temp := temp, i asString, ' ']] timeToRun.
> The time taken to compute is a few milliseconds , but the program freezes for 
> quite a few seconds, What can be the problem ?


Does the same happen when you run the loop with a simple computation? E.g. x := 
x + i.

Re: [Pharo-dev] Freeze while running a loop

2015-04-03 Thread Jigyasa Grover
Hi Gaurav :)
I tried the same code snippet, but it works fine in my system.
It doesn't hang.
Thanks and Regards
Jigyasa Grover

On Fri, Apr 3, 2015 at 5:13 PM, Gaurav Singh  wrote:

> My window freezes whenever i try to run a loop which has even 10
> iterations which are quite less for a computational device.
> Let say i run :
> [| temp |
> temp := String new.
> (1 to: 10)
> do: [:i | temp := temp, i asString, ' ']] timeToRun.
> The time taken to compute is a few milliseconds , but the program freezes
> for quite a few seconds, What can be the problem ?
>