holli has asked for the wisdom of the Perl Monks concerning the following question:
that describes the fields of a fixed length textfile, write a program that constructs a string that can be fed into unpack to get the values out of a line of data.my @fields = ( [ name1 => { start => 0, len=> 10 } ], [ name2 => { start => 8, len=> 3 } ], [ name3 => { start => 11, len=> 14 } ], );
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Challenge: Construct an unpack string
by atcroft (Abbot) on Sep 26, 2006 at 17:00 UTC | |
Assuming this is being extracted from a single text string, and that the positions overlap as shown, then, given the string '0123456789abcdefghijklmnopqrstuvwxyz', the following would seem to extract the data as required: <Reveal this spoiler or all in this thread>
Results:
Does that take conform to what you expected? | [reply] [d/l] [select] |
by holli (Abbot) on Sep 27, 2006 at 11:56 UTC | |
My solution looks like the following (data structure is slightly modified). Also there is a benchmark against Browser_UK's solution: Results: So it looks like the unpack version is faster, as my guts have said. The cost of assembling the unpack string can be considered irrelevant, because in a real world it would happen only once per file. holli, /regexed monk/ | [reply] [d/l] [select] |
by BrowserUk (Patriarch) on Sep 27, 2006 at 13:44 UTC | |
Here's another benchmark.
If your files are bigger than a few lines, the unpack version starts winning quite quickly. But by nowhere near as much as the other benchmarks would have you believe because once you factor in reading each line from the file, the IO time which is constant for both approaches, the parsing time becomes much less significant. Worth having, but much less than you thought. Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
| [reply] [d/l] |
|
Re: Challenge: Construct an unpack string
by BrowserUk (Patriarch) on Sep 26, 2006 at 17:28 UTC | |
Given the form of the input and that the fields can overlap (assuming that was intentional), using unpack for this is awkward and inefficient. You have to parse the input spec. from it's absolute pos/length pairs into the relative form (length/backspace) form required by unpack and build the string template. But the unpack has to parse the string template and convert that back into the position/offset pairs to perform the extractions. Much simpler, (and with having benchmarked, I'll bet quicker), to use substr for this directly: <Reveal this spoiler or all in this thread>
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
| [reply] [d/l] |
by atcroft (Abbot) on Sep 26, 2006 at 18:50 UTC | |
Actually, if you agree that the following code would be valid for a benchmark, then you might be surprised. Code: <Reveal this spoiler or all in this thread>
Results (1_000_000 iterations):
I must say I was surprised. I included the times for "setup"-that is, everything but the operation itself, on the idea that perhaps the setup might have taken longer for the one method than another. Thoughts....? Update (26-Sept-2006): Updated formatting of results. Update (26-Sept-2006): Fixed typo. (Thanks to BrowserUk for pointing it out.) Update (26-Sept-2006): Fixed 2nd typo. (Thanks to chargrill for pointing it out.) | [reply] [d/l] [select] |
by BrowserUk (Patriarch) on Sep 26, 2006 at 19:17 UTC | |
If you hardcode the template to unpack, of course it will be quicker--but when the input data changes, the template won't without programmer intervention--which kinda negates the value of the benchmark. Actually, your entire benchmark is pretty suspect and you are comparing a bunch of entirely different things: So, no. I wouldn't agree that this is a valid benchmark :) Sorry. Also, please use cmpthese for benchmarking, the output from timethese is all but unintelligable. Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
| [reply] [d/l] |
by BrowserUk (Patriarch) on Sep 26, 2006 at 19:22 UTC | |
What the hell is Q?
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
| [reply] [d/l] [select] |
|
Re: Challenge: Construct an unpack string
by jwkrahn (Abbot) on Sep 26, 2006 at 18:04 UTC | |
| [reply] [d/l] |
by ambrus (Abbot) on Sep 27, 2006 at 10:42 UTC | |
Why don't you use the @ template instead of x and X? | [reply] [d/l] [select] |
|
Re: Challenge: Construct an unpack string
by eff_i_g (Curate) on Sep 26, 2006 at 17:08 UTC | |
| [reply] |
|
Re: Challenge: Construct an unpack string
by hobbs (Monk) on Sep 27, 2006 at 21:02 UTC | |
| [reply] [d/l] |