Freeze while running a loop

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
11 messages Options
Reply | Threaded
Open this post in threaded view
|

Freeze while running a loop

Gaurav Singh
My window freezes whenever i try to run a loop which has even 100000 iterations which are quite less for a computational device.
Let say i run :
[| temp |
temp := String new.
(1 to: 100000)
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 ?
Reply | Threaded
Open this post in threaded view
|

Re: Freeze while running a loop

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 <[hidden email]> wrote:
My window freezes whenever i try to run a loop which has even 100000 iterations which are quite less for a computational device.
Let say i run :
[| temp |
temp := String new.
(1 to: 100000)
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 ?

Reply | Threaded
Open this post in threaded view
|

Re: Freeze while running a loop

Max Leske
In reply to this post by Gaurav Singh

On 03 Apr 2015, at 13:43, Gaurav Singh <[hidden email]> wrote:

My window freezes whenever i try to run a loop which has even 100000 iterations which are quite less for a computational device.
Let say i run :
[| temp |
temp := String new.
(1 to: 100000)
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.
Reply | Threaded
Open this post in threaded view
|

Re: Freeze while running a loop

Aliaksei Syrel
In reply to this post by Gaurav Singh
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: 100000)
do: [:i | stream << (i asString, ' ')].
stream contents "returns your temp string"

Cheers,
Alex

On Fri, Apr 3, 2015 at 1:43 PM, Gaurav Singh <[hidden email]> wrote:
My window freezes whenever i try to run a loop which has even 100000 iterations which are quite less for a computational device.
Let say i run :
[| temp |
temp := String new.
(1 to: 100000)
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 ?

Reply | Threaded
Open this post in threaded view
|

Re: Freeze while running a loop

Gaurav Singh
In reply to this post by Max Leske
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 ?
Reply | Threaded
Open this post in threaded view
|

Re: Freeze while running a loop

Gaurav Singh
Thanks Aliaksei , I got it :)

On Fri, Apr 3, 2015 at 5:33 PM, Gaurav Singh <[hidden email]> 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 ?

Reply | Threaded
Open this post in threaded view
|

Re: Freeze while running a loop

Jigyasa Grover
In reply to this post by Gaurav Singh
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 <[hidden email]> 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 ?

Reply | Threaded
Open this post in threaded view
|

Re: Freeze while running a loop

Max Leske

On 03 Apr 2015, at 14:05, Jigyasa Grover <[hidden email]> 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 <[hidden email]> 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 ?


Reply | Threaded
Open this post in threaded view
|

Re: Freeze while running a loop

Jigyasa Grover
Yep , quite possible.

On Fri, Apr 3, 2015 at 5:37 PM, Max Leske <[hidden email]> wrote:

On 03 Apr 2015, at 14:05, Jigyasa Grover <[hidden email]> 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 <[hidden email]> 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 ?



Reply | Threaded
Open this post in threaded view
|

Re: Freeze while running a loop

Alain Rastoul-2
In reply to this post by Aliaksei Syrel
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


Reply | Threaded
Open this post in threaded view
|

Re: Freeze while running a loop

Gaurav Singh
Thanks Alain :)

On Sat, Apr 4, 2015 at 5:02 PM, Alain Rastoul <[hidden email]> 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