Skip to content
Tcl Source Code Ticket Change Details
Login

Ticket Change Details

Overview

Artifact ID: 4f048532aede1b91ad42948895324e572196babf
Ticket: b43f2b49f75ec8b60c7e5b59a21556528657e77a
Non-linear lappend performance
User & Date: dkf on 2014-07-15 22:21:18
Changes

  1. icomment:
    By linear, I mean that the overall time should be linear in the number of items, i.e. O(1) <tt>lappend</tt>. What we've got isn't.
    <p>
    Using the ActiveTcl distro of 8.6.1 (not to pick on them, but it's common in the wild) I see:
    <pre>
    % time {apply {x {for {set i 0} {$i<$x} {incr i} {lappend l x}}} 1000} 1000
    67.750023 microseconds per iteration
    % time {apply {x {for {set i 0} {$i<$x} {incr i} {lappend l x}}} 3000} 1000
    188.477614 microseconds per iteration
    % time {apply {x {for {set i 0} {$i<$x} {incr i} {lappend l x y z}}} 1000} 1000
    3220.6352030000003 microseconds per iteration
    % time {apply {x {for {set i 0} {$i<$x} {incr i} {lappend l x}}} 50000} 1000
    3301.317531 microseconds per iteration
    % time {apply {x {for {set i 0} {$i<$x} {incr i} {lappend l x y}}} 1500} 1000
    5049.768402 microseconds per iteration
    </pre>
    In short, the time to assemble a 3k-element list, 3 elements at a time, is more than 15 times slower than doing it by individual elements, and is in fact similar to assembling a 50k-element list by single <tt>lappend</tt>s. (Something is definitely going wrong, especially as the problem gets worse with longer lists; see the last timing run, which is also assembling a 3k-element list.)
    <p>
    The real "customer" code, shown to me at EuroTcl by Alexandru Dadalau (and which I can't reproduce here as it was tied to a complex product), had much larger lists; it was of a script that was processing a 3D geometry file so that <i>in silico</i> engineering simulations could be run on the tool parts described. (I think.) It was taking over 20 minutes to load a netlist when using a multi-value , and he had "optimized" it by using a chunk of horrible <tt>append</tt>-based string code instead (taking the processing time to around a minute on the same input). Switching to using single-value <tt>lappend</tt>s accelerated the code to where it took 5-10 seconds…
    <p>
    The lack of access to the original code shouldn't matter. The timing runs above reproduce the problem perfectly adequately. (My current near-trunk builds aren't suitable for timing problem hunting; they've got full debugging enabled and so take around 100 times longer anyway.)
    
  2. login: "dkf"
  3. mimetype: "text/html"
  4. username: "dkf"