Skip to content

Count computed property #49

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 11, 2022
Merged

Count computed property #49

merged 1 commit into from
Mar 11, 2022

Conversation

laclouis5
Copy link
Contributor

@laclouis5 laclouis5 commented Mar 10, 2022

Currently the count property is derived from the Sequence default implementation which is O(n). I suggest implementing the count of a PythonObject as the value returned by Python.len() :

  • It will be faster to access the count of an object
  • It will fail is the object has no length

This change could speed-up code that relies on indices and use count :

import PythonKit

let array = [Int](repeating: 0, count: 1_000_000_000).pythonObject
let half = array.count / 2  // Currently a O(n) operation
let elements = array[..<half]

Maybe other performance critical Sequence conformances could be supplied as well, such as underestimatedCount, or Collection's distance().

@pvieito pvieito merged commit d383a1b into pvieito:master Mar 11, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants