{"action":"created","issue":{"url":"https://api.github.com/repos/JuliaLang/julia/issues/11873","labels_url":"https://api.github.com/repos/JuliaLang/julia/issues/11873/labels{/name}","comments_url":"https://api.github.com/repos/JuliaLang/julia/issues/11873/comments","events_url":"https://api.github.com/repos/JuliaLang/julia/issues/11873/events","html_url":"https://github.com/JuliaLang/julia/issues/11873","id":91145784,"number":11873,"title":"Optimize sum(::BitArray)/countnz(::BitArray)","user":{"login":"mtrbean","id":222778,"avatar_url":"https://avatars.githubusercontent.com/u/222778?v=3","gravatar_id":"","url":"https://api.github.com/users/mtrbean","html_url":"https://github.com/mtrbean","followers_url":"https://api.github.com/users/mtrbean/followers","following_url":"https://api.github.com/users/mtrbean/following{/other_user}","gists_url":"https://api.github.com/users/mtrbean/gists{/gist_id}","starred_url":"https://api.github.com/users/mtrbean/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mtrbean/subscriptions","organizations_url":"https://api.github.com/users/mtrbean/orgs","repos_url":"https://api.github.com/users/mtrbean/repos","events_url":"https://api.github.com/users/mtrbean/events{/privacy}","received_events_url":"https://api.github.com/users/mtrbean/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/JuliaLang/julia/labels/performance","name":"performance","color":"d7e102"},{"url":"https://api.github.com/repos/JuliaLang/julia/labels/speculative","name":"speculative","color":"999999"}],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":5,"created_at":"2015-06-26T03:59:42Z","updated_at":"2015-06-26T20:55:27Z","closed_at":null,"body":"Do we have a function to count number of set bit in a BitArray, possibly along a dimension?"},"comment":{"url":"https://api.github.com/repos/JuliaLang/julia/issues/comments/115877756","html_url":"https://github.com/JuliaLang/julia/issues/11873#issuecomment-115877756","issue_url":"https://api.github.com/repos/JuliaLang/julia/issues/11873","id":115877756,"user":{"login":"carlobaldassi","id":223753,"avatar_url":"https://avatars.githubusercontent.com/u/223753?v=3","gravatar_id":"","url":"https://api.github.com/users/carlobaldassi","html_url":"https://github.com/carlobaldassi","followers_url":"https://api.github.com/users/carlobaldassi/followers","following_url":"https://api.github.com/users/carlobaldassi/following{/other_user}","gists_url":"https://api.github.com/users/carlobaldassi/gists{/gist_id}","starred_url":"https://api.github.com/users/carlobaldassi/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/carlobaldassi/subscriptions","organizations_url":"https://api.github.com/users/carlobaldassi/orgs","repos_url":"https://api.github.com/users/carlobaldassi/repos","events_url":"https://api.github.com/users/carlobaldassi/events{/privacy}","received_events_url":"https://api.github.com/users/carlobaldassi/received_events","type":"User","site_admin":false},"created_at":"2015-06-26T20:55:27Z","updated_at":"2015-06-26T20:55:27Z","body":"For the record, when I wrote that I also tried the tricks described here: http://www.hackersdelight.org/hdcodetxt/popArrayHS.c.txt, which in principle could produce some gains on large inputs, but I couldn't measure any impact. It was some time ago though.\r\n\r\nWith respect to counting along a dimension, there surely are optimization opportunities there for some cases (e.g. counting along columns). I just did some very quick tests on random matrices, and it seems some quite large speedups (even 10x on large instances) can be easily obtained like this:\r\n\r\n```julia\r\nInt[sum(a[:,i]) for i = 1:size(a,2)]'  # instead of sum(a,1)\r\n(b=a';Int[sum(b[:,i]) for i =1:size(b,2)]) # instead of sum(a,2)\r\n```\r\n\r\nBoth versions allocate memory though (the second one more)."}}