Talk About Network



Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > Awk > Re: Less greedy...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 25 of 33 Topic 2223 of 2236
Post > Topic >>

Re: Less greedy pattern match

by Ed Morton <morton@[EMAIL PROTECTED] > Apr 21, 2008 at 09:16 AM

On 4/20/2008 9:30 AM, Janis Papanagnou wrote:
> pk wrote:
> 
>>On Saturday 19 April 2008 18:53, Janis Papanagnou wrote:
>>
>>
>>
>>>>{for(i=m+1;i<=NF;i+=2){printf "%s",$i};
>>>>if (NF) {m=(NF%2==m)?1:0}
>>>>if (m==0) print""}
>>>>
>>>>Not elegant as the gawk solution, but at least it should be standard.
>>>>
>>>
>>>Does removing some more characters make it a bit more elegant...?
>>>
>>>  {for(i=m+1;i<=NF;i+=2) printf "%s",$i}
>>>  NF {m=(NF%2==m)}
>>>  !m {print""}
>>
>>
>>Well yes...would that improve performance somewhat too?
> 
> 
> Haven't checked. Would be interesting to know, though; because
> generally I prefer  cond{action}  instead of  {if(cond)action}
> constructs when using awk (though primarily not for efficiency
> reasons).

Run on a 1-million line file with 11 fields per line where each line
starts with
the line number (ran each command 3 times, results concatenated):

$ time awk '/^1/{print}' big1mil > /dev/null
real    0m0.859s user    0m0.749s sys     0m0.124s
real    0m0.843s user    0m0.733s sys     0m0.155s
real    0m0.844s user    0m0.796s sys     0m0.077s

$ time awk '{if (/^1/) print}' big1mil > /dev/null
real    0m0.860s user    0m0.780s sys     0m0.093s
real    0m0.875s user    0m0.796s sys     0m0.093s
real    0m0.937s user    0m0.811s sys     0m0.093s

$ time awk '/^1/{print}' big1mil > /dev/null
real    0m0.844s user    0m0.780s sys     0m0.093s
real    0m0.890s user    0m0.811s sys     0m0.077s
real    0m0.922s user    0m0.827s sys     0m0.046s

-----------------------------------------------

$ time awk '{m=(NF%2==m)?1:0}' big1mil > /dev/null
real    0m1.609s user    0m1.405s sys     0m0.108s
real    0m1.485s user    0m1.405s sys     0m0.109s
real    0m1.484s user    0m1.436s sys     0m0.077s

$ time awk '{m=(NF%2==m)}' big1mil > /dev/null
real    0m1.469s user    0m1.327s sys     0m0.155s
real    0m1.484s user    0m1.405s sys     0m0.109s
real    0m1.469s user    0m1.405s sys     0m0.077s

$ time awk '{m=(NF%2==m)?1:0}' big1mil > /dev/null
real    0m1.469s user    0m1.452s sys     0m0.046s
real    0m1.484s user    0m1.436s sys     0m0.061s
real    0m1.485s user    0m1.390s sys     0m0.108s

Can't say I see any conclusive evidence for either approach being more
efficient, but I may be missing something.

Like Janis says, though, efficiency isn't the main reason to structure
your awk
code to look like awk...

	Ed.




 33 Posts in Topic:
Less greedy pattern match
Prateek <prateek.a@[EM  2008-04-15 19:59:12 
Re: Less greedy pattern match
Ed Morton <morton@[EMA  2008-04-15 22:23:49 
Re: Less greedy pattern match
"Rajan" <svr  2008-04-16 00:02:06 
Re: Less greedy pattern match
pk <pk@[EMAIL PROTECTE  2008-04-17 20:32:10 
Re: Less greedy pattern match
pk <pk@[EMAIL PROTECTE  2008-04-17 21:12:47 
Re: Less greedy pattern match
Ed Morton <morton@[EMA  2008-04-17 14:33:44 
Re: Less greedy pattern match
pk <pk@[EMAIL PROTECTE  2008-04-17 22:01:46 
Re: Less greedy pattern match
pk <pk@[EMAIL PROTECTE  2008-04-17 22:12:04 
Re: Less greedy pattern match
"Rajan" <svr  2008-04-17 19:20:05 
Re: Less greedy pattern match
Ed Morton <morton@[EMA  2008-04-17 23:13:31 
Re: Less greedy pattern match
Ed Morton <morton@[EMA  2008-04-17 23:36:59 
Re: Less greedy pattern match
pk <pk@[EMAIL PROTECTE  2008-04-18 09:50:29 
Re: Less greedy pattern match
Ed Morton <morton@[EMA  2008-04-18 09:27:00 
Re: Less greedy pattern match
pk <pk@[EMAIL PROTECTE  2008-04-18 19:05:23 
Re: Less greedy pattern match
pk <pk@[EMAIL PROTECTE  2008-04-18 19:24:27 
Re: Less greedy pattern match
pk <pk@[EMAIL PROTECTE  2008-04-18 22:06:17 
Re: Less greedy pattern match
Ed Morton <morton@[EMA  2008-04-18 21:19:26 
Re: Less greedy pattern match
Cesar Rabak <csrabak@[  2008-04-19 13:15:05 
Re: Less greedy pattern match
Ed Morton <morton@[EMA  2008-04-20 08:36:44 
Re: Less greedy pattern match
"Rajan" <svr  2008-04-20 09:58:54 
Re: Less greedy pattern match
"Rajan" <svr  2008-04-20 10:21:46 
Re: Less greedy pattern match
Janis Papanagnou <Jani  2008-04-19 18:53:19 
Re: Less greedy pattern match
pk <pk@[EMAIL PROTECTE  2008-04-20 14:32:54 
Re: Less greedy pattern match
Janis Papanagnou <Jani  2008-04-20 16:30:25 
Re: Less greedy pattern match
Ed Morton <morton@[EMA  2008-04-21 09:16:17 
Re: Less greedy pattern match
pk <pk@[EMAIL PROTECTE  2008-04-22 10:09:12 
Re: Less greedy pattern match
Ed Morton <morton@[EMA  2008-04-22 06:14:10 
Re: Less greedy pattern match
"Rajan" <svr  2008-04-18 06:28:14 
Re: Less greedy pattern match
Prateek <prateek.a@[EM  2008-04-15 21:21:23 
Re: Less greedy pattern match
"Rajan" <svr  2008-04-16 00:54:18 
Re: Less greedy pattern match
Ed Morton <morton@[EMA  2008-04-16 07:06:52 
Re: Less greedy pattern match
"Rajan" <svr  2008-04-16 18:01:13 
Re: Less greedy pattern match
Ed Morton <morton@[EMA  2008-04-16 23:48:21 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Fri May 16 6:39:01 CDT 2008.