Title: The count method on strings, bytes, bytearray etc. can be significantly faster · Issue #120397 · python/cpython · GitHub
Open Graph Title: The count method on strings, bytes, bytearray etc. can be significantly faster · Issue #120397 · python/cpython
X Title: The count method on strings, bytes, bytearray etc. can be significantly faster · Issue #120397 · python/cpython
Description: Feature or enhancement Proposal: Counting single characters in a string is very useful. For instance calculating the GC content in a DNA sequence. def gc_content(sequence: str) -> int: upper_seq = sequence.upper() a_count = upper_seq.cou...
Open Graph Description: Feature or enhancement Proposal: Counting single characters in a string is very useful. For instance calculating the GC content in a DNA sequence. def gc_content(sequence: str) -> int: upper_seq = ...
X Description: Feature or enhancement Proposal: Counting single characters in a string is very useful. For instance calculating the GC content in a DNA sequence. def gc_content(sequence: str) -> int: upper_seq...
Opengraph URL: https://github.com/python/cpython/issues/120397
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"The count method on strings, bytes, bytearray etc. can be significantly faster","articleBody":"# Feature or enhancement\n\n### Proposal:\n\nCounting single characters in a string is very useful. For instance calculating the GC content in a DNA sequence.\n```python3\ndef gc_content(sequence: str) -> int:\n upper_seq = sequence.upper()\n a_count = upper_seq.count('A')\n c_count = upper_seq.count('C')\n g_count = upper_seq.count('G')\n t_count = upper_seq.count('T')\n # Unknown N bases should not influence the GC content, do not use len(sequence)\n total = a_count + c_count + g_count + t_count \n return (c_count + g_count) / total\n```\nAnother example would be counting newline characters. \n\nThe current code counts one character at the time.\n```\nstatic inline Py_ssize_t\nSTRINGLIB(count_char)(const STRINGLIB_CHAR *s, Py_ssize_t n,\n const STRINGLIB_CHAR p0, Py_ssize_t maxcount)\n{\n Py_ssize_t i, count = 0;\n for (i = 0; i \u003c n; i++) {\n if (s[i] == p0) {\n count++;\n if (count == maxcount) {\n return maxcount;\n }\n }\n }\n return count;\n}\n```\nBy providing the appropriate hints to the compiler, the function can be sped up significantly. \n\n\n\n### Has this already been discussed elsewhere?\n\nThis is a minor feature, which does not need previous discussion elsewhere\n\n### Links to previous discussion of this feature:\n\n_No response_\n\n\u003c!-- gh-linked-prs -->\n### Linked PRs\n* gh-120398\n* gh-120455\n\u003c!-- /gh-linked-prs -->\n","author":{"url":"https://github.com/rhpvorderman","@type":"Person","name":"rhpvorderman"},"datePublished":"2024-06-12T11:45:05Z","url":"https://github.com/python/cpython/issues/120397"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:d88461f6-28b9-84b7-d22f-2c42438db79c |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | AE14:2E3FEE:45CC4EA:5831D98:6B023312 |
| html-safe-nonce | f43933ab216f72bcba75d1fe9af0055ef1f176f6c2db2d1c7cc09266448515d0 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBRTE0OjJFM0ZFRTo0NUNDNEVBOjU4MzFEOTg6NkIwMjMzMTIiLCJ2aXNpdG9yX2lkIjoiNTgwMzE1MTMzODczMjkyNTcxNCIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | d84e39c769d807f71f973ce777349e60cd5fc083978a801f5ce530a0b4af5995 |
| hovercard-subject-tag | issue:2348562724 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/python/cpython/120397/issue_layout |
| twitter:image | https://opengraph.githubassets.com/21adca9ea4d308aaea185a665c5ac657c72a4650f3be7b2851e2b8c18f53a9ad/python/cpython/issues/120397 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/21adca9ea4d308aaea185a665c5ac657c72a4650f3be7b2851e2b8c18f53a9ad/python/cpython/issues/120397 |
| og:image:alt | Feature or enhancement Proposal: Counting single characters in a string is very useful. For instance calculating the GC content in a DNA sequence. def gc_content(sequence: str) -> int: upper_seq = ... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | rhpvorderman |
| hostname | github.com |
| expected-hostname | github.com |
| None | 87e774ee7c6b3bee0490394e4d3e57ec9dbc956f74b87ea685dc8996c1676456 |
| turbo-cache-control | no-preview |
| go-import | github.com/python/cpython git https://github.com/python/cpython.git |
| octolytics-dimension-user_id | 1525981 |
| octolytics-dimension-user_login | python |
| octolytics-dimension-repository_id | 81598961 |
| octolytics-dimension-repository_nwo | python/cpython |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 81598961 |
| octolytics-dimension-repository_network_root_nwo | python/cpython |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 423dbb54c5e7bfecc838eb387c1435eb3d0e760d |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width