This method is fast, as the following benchmark shows:

Yeah, but it's also wrong, as the following code shows:

#!/usr/bin/perl use strict; use warnings; use Benchmark qw( cmpthese ); my $count = shift || 100000; my @test_array = (0 .. 99); my (@array1, @array2); sub map_bench { @array1 = map { '?' } @test_array; } sub x_bench { @array2 = '?' x @test_array; } map_bench; x_bench; print "\@test_array has ", scalar @test_array, " elements\n"; print "\@array1 has ", scalar @array1, " elements\n"; print "\@array2 has ", scalar @array2, " elements\n"; __END__ @test_array has 100 elements @array1 has 100 elements @array2 has 1 elements

The x operator will only return a multi-element list if all of the following conditions are met:

And the last condition isn't met. Here's a better benchmark, x is still winning, but not with such a margin:
#!/usr/bin/perl use strict; use warnings; use Benchmark qw /cmpthese/; our @test_array = (0 .. 99); our (@array1, @array2); cmpthese -2 => { map => '@::array1 = map {"?"} @::test_array', x => '@::array2 = ("?") x @::test_array' }; die "Benchmark failed" unless @test_array == @array1 && @test_array == @array2; __END__ Benchmark: running map, x for at least 2 CPU seconds... map: 3 wallclock secs ( 2.09 usr + 0.03 sys = 2.12 CPU) @ 46 +08.49/s (n=9770) x: 2 wallclock secs ( 2.28 usr + 0.00 sys = 2.28 CPU) @ 14 +508.33/s (n=33079) Rate map x map 4608/s -- -68% x 14508/s 215% --

Abigail


In reply to Re: Fast Building of SQL Statements by Abigail-II
in thread Fast Building of SQL Statements by hardburn

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.