[cgiapp] Variable Number of Column in a Table

Joshua Miller unrtst at gmail.com
Thu May 15 14:50:23 EDT 2008


I was curious if "may be" was true, and if "pretty slow" was actually
significant, so I did a quick benchmark (and yes, it is  much slower, but
depending on your app, that may not matter).

I've attached the test script, but incase it doesn't come through...
First test, pure perl loop over array of arrays, 100 rows, 500 columns per
row, building an HTML table.
Second test, HTML::Template, TMPL_LOOP over the rows, with 500 statically
defined columns as TMPL_VAR's (again, 100 rows, 500 columns).
Third test, HTML::Template, TMPL_LOOP over the rows, and an inner TMPL_LOOP
over the columns - 100 rows, 500 columns.

40 iterations, Benchmark.pm reports:
Benchmark: timing 40 iterations of perl, tmpl1loop, tmpl2loops...
      perl:  0 wallclock secs ( 0.68 usr +  0.01 sys =  0.69 CPU) @ 57.97/s
(n=40)
            (warning: too few iterations for a reliable count)
 tmpl1loop: 12 wallclock secs (11.72 usr +  0.06 sys = 11.78 CPU) @  3.40/s
(n=40)
tmpl2loops: 40 wallclock secs (39.34 usr +  0.08 sys = 39.42 CPU) @  1.01/s
(n=40)


So, with two loops, it was about 1second per iteration. If you really need
the speed though, removing the templates (and the more complex data
structure) sped things up considerably.

Let me know if the attachments don't come through and you want a copy.
--
Josh I.




On Thu, May 15, 2008 at 12:29 PM, Sean Davis <sdavis2 at mail.nih.gov> wrote:

> On Thu, May 15, 2008 at 12:21 PM, Joshua Miller <unrtst at gmail.com> wrote:
> > Yes, but it's not pretty...
>
> And it may be pretty slow for largish result sets.
>
> You could also consider using client-side javascript to do the table
> layout and sending the data as JSON, but that obviously adds a level
> of complexity.
>
>
> >  <TMPL_LOOP NAME=SQL_OUTPUT>
> >   <TMPL_IF NAME="__first__">
> >     <TR>
> >     <TMPL_LOOP NAME=COLS>
> >       <TH>Headers go here</TH>
> >     </TMPL_LOOP>
> >     </TR>
> >   </TMPL_IF>
> >
> >   <TMPL_IF NAME="__odd__">
> >     <TR>
> >     <TMPL_LOOP NAME=COLS>
> >       <TD>Row of data in odd row color set</TD>
> >     </TMPL_LOOP>
> >     </TR>
> >   </TMPL_IF>
> >
> >   <TMPL_UNLESS NAME="__odd__">
> >     <TR>
> >     <TMPL_LOOP NAME=COLS>
> >       <TD>Row of data in even row color set</TD>
> >     </TMPL_LOOP>
> >     </TR>
> >   </TMPL_UNLESS>
> >  </TMPL_LOOP>
> >
> > $data = {
> >    SQL_OUTPUT => [
> >        { COLS => [ { CELLDATA => "blah" },
> >                    { CELLDATA => "blah" },
> >                    { CELLDATA => "blah" },
> >                  ] },
> >        { COLS => [ { CELLDATA => "blah" },
> >                    { CELLDATA => "blah" },
> >                    { CELLDATA => "blah" },
> >                  ] },
> >        ],
> >    };
> >
> > --
> > Josh I.
> >
> > On Thu, May 15, 2008 at 12:03 PM, Stephen Carville <
> > stephen.carville at gmail.com> wrote:
> >
> >> Is there any way to have a template where the number of columns is
> >> determined at run time?  I know how to use TMPL_LOOP but that only
> >> lets me vary the number of rows.
> >>
> >> What I'm doing is creating a template for SQL reports so I want a
> >> structure like:
> >>
> >>  <TMPL_LOOP NAME=SQL_OUTPUT>
> >>    <TMPL_IF NAME="__first__">
> >>      Headers go here
> >>    </TMPL_IF>
> >>
> >>    <TMPL_IF NAME="__odd__">
> >>      Row of data in odd row color set
> >>    </TMPL_IF>
> >>
> >>    <TMPL_UNLESS NAME="__odd__">
> >>      Row of data in even row color set
> >>    </TMPL_UNLESS>
> >>  </TMPL_LOOP>
> >>
> >>
> >> --
> >> Stephen Carville
> >>
> >> #####  CGI::Application community mailing list  ################
> >> ##                                                            ##
> >> ##  To unsubscribe, or change your message delivery options,  ##
> >> ##  visit:  http://lists.openlib.org/mailman/listinfo/cgiapp    ##
> >> ##                                                            ##
> >> ##  Web archive:   http://lists.openlib.org/pipermail/cgiapp/   ##
> >> ##  Wiki:          http://cgiapp.erlbaum.net/                 ##
> >> ##                                                            ##
> >> ################################################################
> >>
> >>
> >
> > #####  CGI::Application community mailing list  ################
> > ##                                                            ##
> > ##  To unsubscribe, or change your message delivery options,  ##
> > ##  visit:  http://lists.openlib.org/mailman/listinfo/cgiapp    ##
> > ##                                                            ##
> > ##  Web archive:   http://lists.openlib.org/pipermail/cgiapp/   ##
> > ##  Wiki:          http://cgiapp.erlbaum.net/                 ##
> > ##                                                            ##
> > ################################################################
> >
> >
>
> #####  CGI::Application community mailing list  ################
> ##                                                            ##
> ##  To unsubscribe, or change your message delivery options,  ##
> ##  visit:  http://lists.openlib.org/mailman/listinfo/cgiapp    ##
> ##                                                            ##
> ##  Web archive:   http://lists.openlib.org/pipermail/cgiapp/   ##
> ##  Wiki:          http://cgiapp.erlbaum.net/                 ##
> ##                                                            ##
> ################################################################
>
>


More information about the cgiapp mailing list