Decision
This solved all the problems with my Perl code (plus additional implementation code .... :-)) Enabling both Perl and Python is equally awesome.
use WWW::Curl::Easy;
Thanks to everyone who answered, I really appreciate.
Edit
It seems like the Perl code I use spends most of the time doing http get, for example:
my $start_time = gettimeofday; $request = HTTP::Request->new('GET', 'http://localhost:8080/data.json'); $response = $ua->request($request); $page = $response->content; my $end_time = gettimeofday; print "Time taken @{[ $end_time - $start_time ]} seconds.\n";
Result:
Time taken 74.2324419021606 seconds.
My python code is in comparison:
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) > maxsize: r.close() raise ValueError('Response too large') end = time.time() timetaken = end-start print timetaken
Result:
20.3471381664
In both cases, the sorting time is second. Therefore, first of all, I apologize for the misleading question, and this is another lesson for me to never make assumptions .... :-)
I am not sure what is the best thing about this right now. Perhaps someone can suggest a better way to fulfill the request in perl?
End of editing
This is just a quick question about sorting performance differences in Perl vs Python. It is not a question of which language is better / faster, etc., For the record, I first wrote it in perl, noticed the sorting time, and then tried to write the same thing in python to find out how fast it would be, I just want to know how can i make Perl code run as fast as python code?
Lets say we have the following json:
["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 } ]
Suppose we have a list of about 30k-40k entries that we want to sort by one of the sub-keys. Then we want to create a new array of records arranged under the key.
Perl - takes about 27 seconds
my @list; $decoded = decode_json($page); foreach my $id (sort {$decoded->{$b}->{key5} <=> $decoded->{$a}->{key5}} keys %{$decoded}) { push(@list,{"key"=>$id,"key1"=>$decoded->{$id}{key1}...etc)); }
Python - takes about 6 seconds
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)
For Perl code, I tried using the following settings:
use sort '_quicksort';