title: Python vs perl sort performance
body: <p><strong><em>Solution</em></strong></p>
  
  <p>This solved all issues with my Perl code (plus extra implementation code.... :-) ) In conlusion both Perl and Python are equally awesome. </p>
  
  <pre><code>use WWW::Curl::Easy;
  </code></pre>
  
  <p>Thanks to ALL who responded, very much appreciated. </p>
  
  <p><strong><em>Edit</em></strong></p>
  
  <p>It appears that the Perl code I am using is spending the majority of its time performing the http get, for example:</p>
  
  <pre><code>my $start_time = gettimeofday;
  $request = HTTP::Request-&gt;new('GET', 'http://localhost:8080/data.json');
  $response = $ua-&gt;request($request);
  $page = $response-&gt;content;
  my $end_time = gettimeofday;
  print "Time taken @{[ $end_time - $start_time ]} seconds.\n";
  </code></pre>
  
  <p>The result is:</p>
  
  <pre><code>Time taken 74.2324419021606 seconds.
  </code></pre>
  
  <p>My python code in comparison:</p>
  
  <pre><code>start = time.time()
  r = requests.get('http://localhost:8080/data.json', timeout=120, stream=False)
  
  maxsize = 100000000
  content = ''
  for chunk in r.iter_content(2048):
      content += chunk
      if len(content) &gt; maxsize:
          r.close()
          raise ValueError('Response too large')
  
  end = time.time()
  timetaken = end-start
  print timetaken
  </code></pre>
  
  <p>The result is:</p>
  
  <pre><code>20.3471381664
  </code></pre>
  
  <p>In both cases the sort times are sub second. So first of all I apologise for the misleading question, and it is another lesson for me to never ever make assumptions.... :-)</p>
  
  <p>I'm not sure what is the best thing to do with this question now. Perhaps someone can propose a better way of performing the request in perl?</p>
  
  <p><strong><em>End of edit</em></strong></p>
  
  <p>This is just a quick question regarding sort performance differences in Perl vs Python. This is not a question about which language is better/faster etc, for the record, I first wrote this in perl, noticed the time the sort was taking, and then tried to write the same thing in python to see how fast it would be. I simply want to know, <strong>how can I make the perl code perform as fast as the python code?</strong></p>
  
  <p>Lets say we have the following json:</p>
  
  <pre><code>["3434343424335": {
          "key1": 2322,
          "key2": 88232,
          "key3": 83844,
          "key4": 444454,
          "key5": 34343543,
          "key6": 2323232
      },
  "78237236343434": {
          "key1": 23676722,
          "key2": 856568232,
          "key3": 838723244,
          "key4": 4434544454,
          "key5": 3432323543,
          "key6": 2323232
      }
  ]
  </code></pre>
  
  <p>Lets say we have a list of around 30k-40k records which we want to sort by one of the sub keys. We then want to build a new array of records ordered by the sub key. </p>
  
  <p>Perl - Takes around 27 seconds</p>
  
  <pre><code>my @list;
  $decoded = decode_json($page);
  foreach my $id (sort {$decoded-&gt;{$b}-&gt;{key5} &lt;=&gt; $decoded-&gt;{$a}-&gt;{key5}} keys %{$decoded}) {
      push(@list,{"key"=&gt;$id,"key1"=&gt;$decoded-&gt;{$id}{key1}...etc));
  }
  </code></pre>
  
  <p>Python - Takes around 6 seconds</p>
  
  <pre><code>list = []
  data = json.loads(content)
  data2 = sorted(data, key = lambda x: data[x]['key5'], reverse=True)
  
  for key in data2:
       tmp= {'id':key,'key1':data[key]['key1'],etc.....}
       list.append(tmp)
  </code></pre>
  
  <p>For the perl code, I have tried using the following tweaks:</p>
  
  <pre><code>use sort '_quicksort';  # use a quicksort algorithm
  use sort '_mergesort';  # use a mergesort algorithm
  </code></pre>