Yuri Burger wrote:
> I'am comparing different languages... I have selected a simple task
> (http request headers converter) for this benchmark. See my
> implementation of this task in J, example of input and example of output
> in attachment.
>
> My implementation in J converts about 2000 headers per second (on my
> 3GHz Pentium 4).
> Perl implementation converts about 6600 headers per second
> C++ implementation converts about 10000 headers per second.
>
> Is it possible to improve my J implementation?
These relative timings are pretty much expected for parsing text
strings. No way J can match C++, and Perl is usually simpler and more
efficient.
I was interested in your use of fsm. This is elegant and quite
efficient, but I wonder if it makes the code more complex than
necessary. I don't know enough about http message headers to judge this,
but I tried hacking it directly, taking your source file as input and
trying to get the required output. The only hard part here was parsing
the first line, I did this by simply plodding through it and extracting
values one by one.
The code is below. This gives the same output as yours, except that the
order may differ. My timings were 0.024 vs 0.044 for your method, so
scaling up your results gives about 3600 headers/second.
IFNAME=: jpath '~Other/httphdr/headers.txt'
OFNAME=: jpath '~temp/records.txt'
LHN=: 'hexam'
FIXHDR=: 'GET ';'HOST: '
DEFS=: ('HOST: ';LHN),('PORT: ';'80'),:'PROTOCOL: ';'http'
main=: 3 : 0
dat=. (LF,LF), 1!:1 <IFNAME
dat=. ((LF,LF) E. dat) <@}.;._1 dat
dat=. dat -. a:
txt=. parse each dat
txt=. ; txt ,each LF
txt 1!:2 <OFNAME
)
parse=: 3 : 0
dat=. a: -.~ <;._2 y,LF
ndx=. 1 + dat i. &> ' '
hdr=. toupper each ndx {.each dat
val=. ndx }.each dat
msk=. hdr e. FIXHDR
dat=. ;(msk#hdr) phdr each msk#val
dat=. dat, ((-.msk)#hdr,.val), DEFS
msk=. ~: {."1 dat
;msk # dat,.<LF
)
phdr=: 4 : 0
if. (<x) e. FIXHDR do.
('p',x-.' :')~y
else.
x,y
end.
)
pGET=: 3 : 0
y=. <;._2 y,' '
'p http'=. 2 {. y,<'HTTP/0.9'
r=. ,:'METHOD: ';'GET'
r=. r,'HTTP-PROTOCOL-VERSION: ';http-.LF
if. '/' ~: {.p do.
ndx=. p i. ':'
r=. r,'PROTOCOL: ';ndx {. p
p=. (ndx+3) }. p
ndx=. p i. '/'
hdr=. ndx {. p
p=. ndx }. p
ndx=. hdr i. ':'
r=. r,'HOST: ';ndx {. hdr
if. ndx < #hdr do.
r=. r,'PORT: ';(ndx+1) }. hdr
end.
end.
ndx=. p i. '?'
r=. r,'RESOURCE: ';}. ndx {. p
r,'QUERY: ';(ndx+1) }. p
)
pHOST=: 3 : 0
ndx=. y i. ':'
if. ndx < #y do.
('HOST: ';ndx{.y),:'PORT: ';(ndx+1)}.y
else.
'HOST: ';y
end.
)
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm