Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: Hash vs constant vs package vs other for data structure

by Laurent_R (Canon)
on Mar 27, 2017 at 19:57 UTC ( [id://1186140]=note: print w/replies, xml ) Need Help??


in reply to Hash vs constant vs package vs other for data structure

The best way to store your data really depends on what you're doing with it afterwards. Also a top level hash might be better if your data points are sparse.

Assuming that you'll never use the horizontal coordinate (abscissa) without the vertical coordinate (ordinate), you might even store them as a concatenated value in a hash, thereby simplifying your data structure by removing one level of nested-ness:

my %notes; # notes is now a hash #... $notes{"$x;$y"}[0] = ...
or:
$notes{"$x;$y"}{...} = ...

Replies are listed 'Best First'.
Re^2: Hash vs constant vs package vs other for data structure
by oldtechaa (Beadle) on Mar 27, 2017 at 20:34 UTC
    This is an interesting technique. It would certainly work and I do have a sparse data set, but it doesn't seem clearer. What are the performance impacts of this?
      What are the performance impacts of this?
      It would have to be measured, i.e. bench-marked, with real data.

      However, my gut feeling is that removing one level of nested-ness is likely to speed up things a bit, but probably not by a large margin. I doubt that you really care about the difference for what you're doing. So, don't worry too much about performance, unless you really have to.

      The hash solution (especially with concatenated keys) is very likely to use far less memory, at least with sparse data. Suppose you've got only one data point with coordinates (800, 1200). With an array of arrays, you have to allocate essentially 800 * 1200 array slots, that's quite a lot or memory for just one data piece. But with a hash you need to allocate only one or two hash entries; even considering that a hash entry uses more memory than an array entry, there is a significant win here.

        I may go with this method actually since I see the memory benefits should be quite large. What's more, to get the list of objects all I need to do is get the keys and sort them by the $x value, reducing algorithmic complexity a ton from a full array search. Thanks.
      but it doesn't seem clearer

      Granted, but it makes things simpler (and easier) if you need to traverse your entire data structure. You essentially get a better data abstraction if you think in terms of "location", rather than "x-y coordinates".

      I should probably note here that since I do have a sparse data set, the array members are undefined until needed.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1186140]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (5)
As of 2024-03-29 08:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found