CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
Search Posts SEARCH
Who's Online WHO'S
Log in LOG

Home: Perl Programming Help: Beginner:
Array Optimization


New User

Nov 26, 2015, 5:50 AM

Post #1 of 4 (1837 views)
Array Optimization Can't Post

Hi all,

For a project I am storing information about servers in an array (about 500 servers each one has a string inside the array).

When I am done retrieving data form all the servers I need to print some reports in csv files. One of them store the information of all the servers. And several of them is top 20 of a certain condition (cpu usage, RAM size ...)

So while searching the web I did find out how to make custom sort of an array. But I want to know when to do it.

I have a few possibilies:
-> lazy: when I finish to store all my information inside the array I apply my custom sort one after another and store each time the top 20 line inside another array.

-> half lazy: I create an array for each custom sort and each time I had a line to my final array I also add it to the other one, then I sort them and pop up the line I don't want to keep in them (I call my custom sort a lot more but never on more than 21 lines)

-> not so lazy: I create an array for each custom sort and make sure they are always sorted, every time I add a new line I check if I need to store the line in one of the other arrays and use splice to maintain my arrays sorted. (not sure it will be more efficient that using sort)

This script will run only once a day on a powerful server and maybe optimizing it will not change anything but if I don't care about optimization I will bring shame on all my clan and family.

Thanks in advance for those who will help me.

Veteran / Moderator

Nov 26, 2015, 6:45 AM

Post #2 of 4 (1821 views)
Re: [jeremiez] Array Optimization [In reply to] Can't Post

Frankly, sorting 500 items once a day (even several times) is really peanuts nowadays.

Having said that, I still usually prefer to do things efficiently, so I would probably keep only an array of the ten top items for each criteria. The next question is: do you use sort or do you manually insert the new items in the right position? The second solution is likely to be slightly more efficient, but I would not care too much about that (it would possibly make a difference on an array with thousands of items, not really on an array with 10 items).

$ perl -E '@a = reverse  1..10; say "@a"; @a = (sort {$c++; $b <=> $a} @a, 11)[0..9]; say "@a"; say $c;' 
10 9 8 7 6 5 4 3 2 1
11 10 9 8 7 6 5 4 3 2

As you can see above, adding one element to the array and sorting such array leads to 21 comparisons (value of the $c variable). Manually, you could reduce it to 10 or less array item comparisons with an insert in-place algorithm, but there will be some overhead to compile a slightly more complex program, so that I doubt it would be significantly better. And it is so small, that you can't even benchmark it with any reliable degree of significance.

(This post was edited by Laurent_R on Nov 26, 2015, 6:46 AM)

New User

Nov 26, 2015, 8:14 AM

Post #3 of 4 (1812 views)
Re: [Laurent_R] Array Optimization [In reply to] Can't Post

Thanks for your help.

I will go for the not so lazy solution for now. Maybe someday the number of server will grow from 500 to 500K.


Nov 26, 2015, 9:55 AM

Post #4 of 4 (1805 views)
Re: [jeremiez] Array Optimization [In reply to] Can't Post

Whichever approach you choose, it is not necessary to create and sort a duplicate array. Create and sort an array of references instead.
Good Luck,


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives