Skip to content
Tcl Source Code View Ticket
Login

View Ticket

improve speed of read command

Closed219228ffffffffffffffffffffffffffffffffffCreated 26 years agoUpdated 25 years ago
Descriptionplain2000-10-26 05:03:51
OriginalBugID: 4206 RFE
Version: 8.2.3
SubmitDate: '2000-02-22'
LastModified: '2000-03-30'
Severity: LOW
Status: Assigned
Submitter: techsupp
ChangedBy: hobbs
OS: Solaris
OSVersion: SunOS 5.7
Machine: Ultra Sparc, 128MB of RAM, Single Processor.
FixedDate: '2000-10-25'
ClosedDate: '2000-10-25'


Name:
David Whittle

Extensions:
Sybtcl-2.5 (although it was not loaded for the test).

CustomShell:
None.

ReproducibleScript:
# The file represented by "test.file" should be a binary file
# with 2+ million records, where a record is defined as 42 bytes.
set fp [open test.file r]
set i 0
set time [clock seconds]
while {[read $fp 42] != 0} {
    if ![expr 50000 % $i] {
        set now [clock seconds]
        puts "[expr $now - $time] secs, $i recs, [expr 50000 / ($now - $time)] recs per sec"
        set time $now
    }
}
close $fp

ObservedBehavior:
The issue that I have found is that running the above script (which is essentially only doing a read) takes ~8 seconds to process 50000 "records". When we compared this against a similar Perl script, Perl out did Tcl by a few orders of magnitude (Perl ran through 200000 in ~1 second).
I looked at the code for the read command and it appears to me that the issue lies in the entended work with the channel code as well as how it appears to attempt to process each byte, even if no conversion is required.
I also tried testing this in 8.1.1 (The above script wouldn't work in 8.0 and below (i'm guessing due to the binary data in the file)) which ran a bit faster (5 seconds for 50,000 records) and 8.3 which ran about the same speed as 8.2.3.

DesiredBehavior:
I would expect unless something has been specified differently in the fconfigure command that it would bubble down to a simple fread command for increased performance on the reads.



I would expect a noticeable speedup if the standard programming
paradigms were followed.  This should be:

proc process {file} {
  set fp [open $file r]
  set i 0
  set time [clock seconds]
  while {[read $fp 42] != 0} {
    if {!(50000 % $i)} {
        set now [clock seconds]
        puts "[expr {$now - $time}] secs, $i recs, [expr {50000 / ($now - $time)}] recs per sec"
        set time $now
    }
  }
  close $fp
}
process test.file

That needs to be tested first, before we evaluate the I/O (which
could likely be improved).  Of course, bulk reads with range
fetches would then be much better. 
-- 02/22/2000 hobbs
dkfplain2000-11-10 21:22:49
Do you get improved performance by specifying that you are reading binary data as opposed to data which needs converting to UTF-8/UNICODE?  If that "fixes" the problem, then the problem is due to Tcl's encoding support, which can have quite a large performance penalty.  In that case, there's not a lot that can be done about it.  You would expect Perl to suffer similarly if it was trying to do encoding stuff though!

A short test later, I find when reading binary data that I get much better performance with...

 proc process {file} { 
    set fp [open $file r]
    fconfigure $fp -translation binary
    set i 0 
    set time [clock seconds] 
    while {[string length [read $fp 42]] != 0} { 
       if {!([incr i] % 50000)} {
          set now [clock seconds] 
          puts "[expr {$now - $time}] secs, $i recs" 
          set time $now 
       } 
    } 
    close $fp 
 }

Unfortunately, the actual figures aren't too good since my computer started swapping...  :^(
hobbs Ticket [219228ffff]: 2 changes [History diff]2001-08-02 08:44:57
andreas_kupries Ticket [219228ffff]: 2 changes [History diff]2001-09-18 01:24:55
andreas_kupriesplain2001-09-18 01:37:49
Logged In: YES 
user_id=75003

David Whittle = <[email protected]>.
Sent mail asking for feedback ...
Log in (anonymous is fine) to add a comment.