Talk:Knuth's algorithm S: Difference between revisions

Line 14:
:''"At each of the 100000 repetitions not only is a new function created but also new copies of its PRIVATE variables index% and samples%(). Creating such a large number of variables at run-time impacts adversely on execution speed and isn't to be recommended, other than to meet the artificial requirements of the task."''
 
I took a look at your example and it seems that function <code>FNs_of_n_creator</code> calculates and keeps each random sample from ten and you keep 100000 of them around before extracting their data for binning.
 
::Not really. The digit counts are accumulated immediately after the created function is called ten times, as the task requires. At that point the function is finished with, but unfortunately BBC BASIC has no way of discarding it: the fact that 100000 functions and their associated private variables are left lying around is a consequence of BBC BASIC's simplistic heap management. It would be possible to reduce memory usage, but only at the expense of complexity and clarity. As a 'real world' application is unlikely to need to create thousands of functions on-the-fly I thought it better for the code to illustrate a more typical and straightforward usage. [[User:RichardRussell|RichardRussell]] 10:14, 5 November 2012 (UTC)
 
The Python example, (amongst others e.g. Ada), bin results as they go along and so don't have to keep so many functions around - maybe the BBC basic example could follow a similar scheme?
Line 22 ⟶ 24:
# Quickly show that the routine can generate a non-biased sample (without using too much stats).
It could well be thought of as artificial but I hope I've explained the purpose. --[[User:Paddy3118|Paddy3118]] 07:45, 5 November 2012 (UTC)
 
::I meant 'artificial' in the requirement to create, in advance, a function taking a single parameter (i.e. introducing a First Class Function element to the task). The actual Knuth S algorithm is embodied in the BBC BASIC function <code>FNs_of_n</code> which can be called directly without any of the memory usage complications, but it takes additional parameters (the sample size and an array to hold the sample). [[User:RichardRussell|RichardRussell]] 10:14, 5 November 2012 (UTC)