From: Robert Klemme Date: 2009-09-01T16:12:32+09:00 Subject: Re: Best Practice for Multiline Regexps 2009/9/1 spiralofhope : > On Tue, 1 Sep 2009 14:55:08 +0900 > Robert Klemme wrote: > >> > 1) Read file to String, match string against first pattern, read >> > next file into String, match with same pattern... once I've gone >> > through all the files with the first pattern, start over with the >> > next pattern. >> >> That's the worst you can do. > >> > 2) Read file to String, match whole string against all 20 patterns, >> > go to next file, match against 20 patterns... >> >> Most efficient of the simple approaches. > > Robert is right from a hard drive perspective. > > To understand why method 2 works well - just remember that when a file > is read from your disk, it is cached.  Your first solution would force > the system to cache file 1, process it, then cache file 2 to process > it.. through to caching file n and then back to file 1 again, cycling > through each pattern you're searching for for every file.  It would be > horribly taxing on disk access. Exactly! > But if we imagine that all the files are on a ramdisk, then could the > first method possibly be better?  Something in the back of my mind > says method 2 is still better. Yes, and here's why: you save the effort of transferring file data from the disk into Ruby's address space (memory mapped IO is OS dependent and might not be available - also, I believe it's not a core lib functionality). Additionally: it is unlikely that all files reside on a ramdisk regularly so you have the additional effort of moving / copying files there. Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/