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 ?
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 <grvanm.coder@gmail.com> 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 ?
On 03 Apr 2015, at 13:43, Gaurav Singh <grvanm.coder@gmail.com> 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.
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 ?
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 <grvanm.coder@gmail.com> 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 ?
On 03 Apr 2015, at 14:05, Jigyasa Grover <grover.jigyasa1@gmail.com> 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 <grvanm.coder@gmail.com <mailto:grvanm.coder@gmail.com>> 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 ?
Yep , quite possible. On Fri, Apr 3, 2015 at 5:37 PM, Max Leske <maxleske@gmail.com> wrote:
On 03 Apr 2015, at 14:05, Jigyasa Grover <grover.jigyasa1@gmail.com> 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 <grvanm.coder@gmail.com> 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 ?
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 <grvanm.coder@gmail.com> 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 ?
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
Thanks Alain :) On Sat, Apr 4, 2015 at 5:02 PM, Alain Rastoul <alf.mmm.cat@gmail.com> 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
participants (5)
-
Alain Rastoul -
Aliaksei Syrel -
Gaurav Singh -
Jigyasa Grover -
Max Leske