![]() |
|
No such thing as a small change | |
PerlMonks |
Do you really want to use an array there?by deprecated (Priest) |
on Jun 12, 2004 at 05:19 UTC ( #363563=perlmeditation: print w/replies, xml ) | Need Help?? |
Being a perl hacker, you may twitch and feel uncomfortable reading this node. It really is worth your time, so try to sit through it.
I like to start with code, so let's have a look at this: Take a moment, if you dare, to look over the perldoc for vec. I think, like most perl hackers, that looking at that particular page, you'll see some perl 4 ism's and some particularly unuseful code, and think, gee, this is right up there with Larry using ' instead of ::. I don't need that garbage. Please, read the above again, and see how simple vec is to use. Recently, I've been working with obscenely large arrays. One of the problems I've bumped into is one of our user-developers has said that they have occasionally an array so large that they cannot fit two copies of it into memory. Additionally, any time you iterate over the array, you find that passing references to the array and accessing elements individually is also time consuming. Enter the vector. It is helpful to think of a vector as a joined array (on ''). Essentially, it is as if you had done something like: Wouldn't you rather use substr() (note: substr is fast!!)to refer to individual bytes of that "array" than to pass references around and deref them, or point at individual elements of it? String functions can be a lot faster than list operations. In fact, this is so important that many computers now ship with "vector units" (Crays and Macs among them). vec() is essentially a pretty wrapper around the C vector functions (with a little endian assumption. If you've compiled your perl, say, -faltivec, you'll have access to those units from within perl). Vectors also allow you to perform bitwise operations on entire vectors at once, rather than walking the array (see vec). That is substantially faster. If you don't think this is a substantial improvement, think about asking perl to my @foo = @{ $bar } and how much cpu that takes to do on a 30 million element array. Providing benchmarks is kind of silly. I have a bunch of machines here with vector processors. My results will be different than yours. I would hope, though, by looking at the above, you can see that it might be worthwhile to actually try using a vector. In many cases, it is more efficient both cpu and memory-wise, than an array. And not much harder to use. My own gains with vectors have been between 5 and 20 percent memory savings, and 30 to 500 percent increases in speed. My goal is not to convince people vectors are better -- they are sometimes, and not others -- but rather to convince people to try to use them. I rarely ever find a perl hacker who knows what a vector is, or why it might be much better to use a vector than an array for their application. vec is a perl builtin. Don't be afraid. happy vectoring. dep. ps: if you find that you really find this attractive you may want to check out PDL and Bit::Vector. --
Back to
Meditations
|
|