in reply to Re: 'ls -C' column style (sideways)
in thread 'ls -C' column style

This one loops over the list of items once (at most).

Intriguing. Both your original version and the updated one do much better than mine at small widths (25), but then get progressively worse, where mine gets better.

I haven't read through your algorithm properly yet, but we do get the same results (except I used 2 spaces not 1 between the columns).

[22:49:46.54] P:\test>405274 >nul 100 trials of Buk:25 ( 190.051ms total), 1.901ms/trial 100 trials of Buk:50 ( 113.853ms total), 1.139ms/trial 100 trials of Buk:75 ( 88.451ms total), 884us/trial 100 trials of Buk:100 ( 70.376ms total), 703us/trial 100 trials of Buk:125 ( 38.077ms total), 380us/trial 100 trials of Buk:150 ( 46.875ms total), 468us/trial 100 trials of Buk:175 ( 46.875ms total), 468us/trial 100 trials of Buk:200 ( 46.875ms total), 468us/trial [22:50:02.71] P:\test>405274-tye1 >nul 100 trials of Tye1:25 ( 93.742ms total), 937us/trial 100 trials of Tye1:50 ( 190.402ms total), 1.904ms/trial 100 trials of Tye1:75 ( 265.551ms total), 2.656ms/trial 100 trials of Tye1:100 ( 343.750ms total), 3.438ms/tria +l 100 trials of Tye1:125 ( 421.875ms total), 4.219ms/tria +l 100 trials of Tye1:150 ( 453.125ms total), 4.531ms/tria +l 100 trials of Tye1:175 ( 468.750ms total), 4.688ms/tria +l 100 trials of Tye1:200 ( 484.375ms total), 4.844ms/tria +l [22:50:08.43] P:\test>405274-tye2 >nul 100 trials of Tye2:25 ( 107.743ms total), 1.077ms/trial 100 trials of Tye2:50 ( 154.991ms total), 1.550ms/trial 100 trials of Tye2:75 ( 200.225ms total), 2.002ms/trial 100 trials of Tye2:100 ( 227.510ms total), 2.275ms/tria +l 100 trials of Tye2:125 ( 250ms total), 2.500ms/tria +l 100 trials of Tye2:150 ( 234.375ms total), 2.344ms/tria +l 100 trials of Tye2:175 ( 234.375ms total), 2.344ms/tria +l 100 trials of Tye2:200 ( 250ms total), 2.500ms/tria +l

Examine what is said, not who speaks.
"Efficiency is intelligent laziness." -David Dunham
"Think for yourself!" - Abigail
"Memory, processor, disk in that order on the hardware side. Algorithm, algorithm, algorithm on the code side." - tachyon

Replies are listed 'Best First'.
Re^3: 'ls -C' column style (sideways)
by tye (Sage) on Nov 05, 2004 at 01:10 UTC

    I didn't write it this way for performance reasons. But this technique should have good performance when sorting items across instead of down.

    You can also make it faster with a bit of complication by teaching it to consider about 2N^.5 column counts rather than about N/2 of them.

    - tye        

      Neither did I, I was just curious about your "This one loops over the list of items once (at most)." claim. Knowing mine makes several passes, I wondered how they compared. I (think) I see that you actually do make 2 passes: 1 to determine the no. of columns; 1 to do the formatting.

      I'm not sure what you mean by the 2N^.5 bit? No matter how I bracket that, it always comes out to 74 compared to 37/2 = 18 or 19.

      The problem with limiting the columns in that way is that you miss the edge case where the entire list will format into 1 row--and split it across 2.

      The reason mine arrives at the answer quicker in all but the smallest widths case, is that I come at from the other end. I iterate the rows 1 .. N, and calculate the columns.

      I noticed that in the general case increasing the number of rows until it fit arrived more quickly than increasing the number of columns as it considered less duplicate cases. For best performance, you could probably determine some formula that would indicate which end to start from, but my math ain't that strong.


      Examine what is said, not who speaks.
      "Efficiency is intelligent laziness." -David Dunham
      "Think for yourself!" - Abigail
      "Memory, processor, disk in that order on the hardware side. Algorithm, algorithm, algorithm on the code side." - tachyon
Re^3: 'ls -C' column style (sideways)
by hv (Prior) on Nov 05, 2004 at 11:45 UTC

    I suspect the most efficient way would be to do a binary chop on the number of rows, with an initial min/max of 1, @items.

    Hugo