4 ms·
I don't see how this is supposedly more efficient than the easier approach of listing all files and choosing a single random number in 1..n
by Sopel 23d ago
I don't see how this is supposedly more efficient than the easier approach of listing all files and choosing a single random number in 1..n
- bonzini 23d agoYou have to allocate memory and free it. Interestingly, when reading Raymond Chen's article I thought "reservoir sampling would compare the random number (between 1 and n) to 1, not to n, because that extends more easily to picking more than one element" - and that's what the actual Windows code uses.
- Sopel 22d agoyes, you would have to allocate space for up to 100 file paths, but the article says > it’s more efficient because it reduces the amount of calls into the file system, which is where the bottleneck is Upon reading it a few times I think the article is alluding to a crappy two-pass solution where you don't store a filename but instead an index into a directory, which is flawed anyway due to being racy.