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.On Fri, Apr 3, 2015 at 5:13 PM, Gaurav Singh <[hidden email]> wrote:
|
In reply to this post by Gaurav Singh
Does the same happen when you run the loop with a simple computation? E.g. x := x + i.
|
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 | Cheers, Alex On Fri, Apr 3, 2015 at 1:43 PM, Gaurav Singh <[hidden email]> wrote:
|
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 ?
|
Thanks Aliaksei , I got it :) On Fri, Apr 3, 2015 at 5:33 PM, Gaurav Singh <[hidden email]> wrote:
|
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:
|
May be memory / hardware dependent.
|
Yep , quite possible. On Fri, Apr 3, 2015 at 5:37 PM, Max Leske <[hidden email]> wrote:
|
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 |
Thanks Alain :) On Sat, Apr 4, 2015 at 5:02 PM, Alain Rastoul <[hidden email]> wrote: Hi Gaurav, |
Free forum by Nabble | Edit this page |