[Shootout-list] Tcl: tcp-echo
Robert Seeger
Robert Seeger <rhseeger@gmail.com>
Wed, 16 Mar 2005 16:03:56 -0500
Nice. The encoding and buffersize options are what do it, as far as I
can tell. I updated my code to include them, and it clocks in even
faster now. My new code is pasted below.
I also pulled the main code out of the proc, since there's no good
reason to bytecompile something that's only run once, and has no loop
constructs.
Rob Seeger
On Wed, 16 Mar 2005 15:27:56 -0500, Randy Melton <rmelton@atmel.com> wrote:
> I posted a similar one yesterday that was held up in moderation.
>
> While I like yours a little better, My prior attempt clocks in slightly faster on my system.
> Maybe the fake fork vs the pipe?
> or setting the io to buffered and setting buffer size?
>
> Randy Melton
>
proc Server {channel clientaddr clientport} {
set reply [string repeat x 64]
fconfigure $channel -buffersize 64 -encoding binary
while { ![eof $channel] } {
read $channel 64
puts -nonewline $channel $reply
}
set ::forever 1
}
proc doChild {num} {
set request [string repeat x 64]
set bytes 0
set replies 0
set fd [socket localhost 9900]
fconfigure $fd -buffersize 64 -encoding binary
set num [expr {$num * 6400}]
while { [incr num -1] >= 0 } {
puts -nonewline $fd $request
incr bytes [string length [read $fd 64]]
incr replies
}
close $fd
puts "replies: $replies\tbytes: $bytes"
exit
}
if { [llength $argv] == 2} {
doChild [lindex $argv 1]
} else {
socket -server Server 9900
exec [info script] Child [lindex $argv 0] &
vwait ::forever
exit
}