Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Because you are sorting lexically, Try this code:</p> <pre><code>#!/usr/bin/perl use strict; use warnings; my $ref = [ 5.0e-5,4.2e-5,4.3e-5,4.4e-5,4.4e-5,4.2e-5,4.2e-5,4.0e-5]; print "Perl with cmp\n"; for my $val (sort @$ref) { printf "%f \n", $val; } print "Perl with &lt;=&gt;\n"; for my $val (sort { $a &lt;=&gt; $b } @$ref) { printf "%f \n", $val; } print "C\n"; test($ref); use Inline C =&gt; &lt;&lt;'END_OF_C_CODE'; void test(SV* sv, ...) { I32 i; I32 arrayLen; AV* data; float retval; SV** pvalue; Inline_Stack_Vars; data = SvUV(Inline_Stack_Item(0)); /* Determine the length of the array */ arrayLen = av_len(data); // sort sortsv(AvARRAY(data),av_len(data)+1,Perl_sv_cmp_locale); arrayLen = av_len(data); for (i = 0; i &lt; arrayLen+1; i++) { pvalue = av_fetch(data,i,0); /* fetch the scalar located at i .*/ retval = SvNV(*pvalue); /* dereference the scalar into a number. */ printf("%f \n",newSVnv(retval)); } } END_OF_C_CODE </code></pre> <p>Of course, lexically <code>0.00040</code> is smaller than <code>0.00042</code> as well, but you aren't comparing <code>0.00040</code> to <code>0.00042</code>; you are comparing the number <code>0.00040</code> converted to a string with the number <code>0.00042</code> converted to a string. When a number gets too large or small, Perl's stringifying logic resorts to using scientific notation. So you are sorting the set of strings </p> <pre><code>"4.2e-05", "4.2e-05", "4.2e-05", "4.3e-05", "4.4e-05", "4.4e-05", "4e-05", "5e-05" </code></pre> <p>which are properly sorted. Perl happily turns those strings back into their numbers when you ask it to with the <code>%f</code> format in <code>printf</code>. You could stringify the numbers yourself, but since you have stated you want this to be faster, that would be a mistake. You should not to be trying to optimize the program before you know where it slow (premature optimization is the root of all evil<code>*</code>). Write your code then run <a href="http://search.cpan.org/dist/Devel-NYTProf/" rel="nofollow noreferrer">Devel::NYTProf</a> against it to find where it is slow. If necessary, rewrite those portions in <a href="http://perldoc.perl.org/perlxstut.html" rel="nofollow noreferrer">XS</a> or <code>Inline::C</code> (I prefer XS). You will find that you get more speed out of choosing the right data structure than micro-optimizations like this.</p> <p><code>*</code> <a href="http://www-cs-faculty.stanford.edu/~knuth/" rel="nofollow noreferrer">Knuth, Donald</a>. <a href="http://portal.acm.org/citation.cfm?id=356640" rel="nofollow noreferrer">Structured Programming with go to Statements</a>, <a href="http://surveys.acm.org/" rel="nofollow noreferrer">ACM Journal Computing Surveys</a>, Vol 6, No. 4, Dec. 1974. p.268.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload