简体   繁体   中英

perl6 Performance difference between @$aList and $aList

I have data structures that are very large and need to be assigned and passed around. I also need to get inside the list. Sometimes when a list is in a scalar container, $aList.elems will say 1 because there is only one element which is the list. To get inside the list, (@$aList).elems will give the correct number of elements in the list.

My question is: is there a performance disadvantage with using @$aList frequently, and if there is a performance issue, will assigning @b = @$aList and use @b instead solve the issue? Ie, will there be speed bump with switching from list to array context?

Thanks.

$x.elems and (@$x).elems should return the same number. So it sounds like there's a bug in your code that you should fix... Possibly something related to Seq caching, see below.

As for the performance question, I haven't run benchmarks, but note that @$x simply calls $x.cache , which...

  • if $x is a List or subclass (which includes Array ), simply returns the object itself (without the surrounding item container), which should be fast.
  • if $x is an object of another type, creates a new List from it and returns that, which will likely have a bit more overhead depending on the type. eg:
    • String "a" --> List ("a",)
    • Range 1..10 --> List (1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
      Obviously, for huge Ranges, coercing to a list will have a significant memory and CPU performance cost.
    • Seq (1, 2).Seq --> List (1, 2)
      The Seq case is special, because it remembers the List object that's created the first time .cache is called on it, and continues to return the same one on subsequent calls. That's why the method is called .cache in the first place.

(Note that the .list method also exists, which does the same thing as .cache except that it doesn't remember the List in the Seq case.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM