{"id":456296,"date":"2025-04-16T15:01:00","date_gmt":"2025-04-16T15:01:00","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=456296"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=456296","title":{"rendered":"<span>\u041e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u044f Go map{-}{-}<\/span>"},"content":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<h3>\u0412\u0432\u0435\u0434\u0435\u043d\u0438\u0435<\/h3>\n<p><strong>\u0425\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0430(\u043c\u0430\u043f\u0430)<\/strong> \u2014 \u043e\u0434\u043d\u0430 \u0438\u0437 \u0441\u0430\u043c\u044b\u0445 \u043f\u043e\u043f\u0443\u043b\u044f\u0440\u043d\u044b\u0445 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440 \u0434\u0430\u043d\u043d\u044b\u0445, \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e \u043f\u043e\u0438\u0441\u043a \u043f\u043e \u043a\u043b\u044e\u0447\u0443 \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442 \u0437\u0430 <strong>O(1)<\/strong>. \u041f\u0440\u0438\u0447\u0435\u043c \u043a\u043b\u044e\u0447 \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u043b\u044e\u0431\u044b\u043c \u043b\u044e\u0431\u044b\u043c \u0442\u0438\u043f\u043e\u043c, \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043a\u043e\u0442\u043e\u0440\u043e\u0433\u043e \u043c\u043e\u0436\u043d\u043e \u0441\u0440\u0430\u0432\u043d\u0438\u0432\u0430\u0442\u044c (Comparable Trait).<\/p>\n<p>\u042f \u0441\u0442\u043e\u043b\u043a\u043d\u0443\u043b\u0441\u044f \u0441 \u0442\u0435\u043c, \u0447\u0442\u043e \u043c\u0430\u043f\u0430 \u043d\u0435 \u0442\u0430\u043a\u0430\u044f \u0431\u044b\u0441\u0442\u0440\u0430\u044f \u043f\u043e \u0431\u0435\u043d\u0447\u043c\u0430\u0440\u043a\u0430\u043c \u043d\u0430 \u044f\u0437\u044b\u043a\u0435 GO, \u0445\u043e\u0442\u044f \u0442\u0435\u043e\u0440\u0435\u0442\u0438\u0447\u0435\u0441\u043a\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430 \u041e(1).<\/p>\n<p>\u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0443\u044e \u0437\u0430\u0434\u0430\u0447\u0443 \u0438 \u0441\u043f\u043e\u0441\u043e\u0431\u044b \u0435\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u044f.<\/p>\n<h3>\u0417\u0430\u0434\u0430\u0447\u0430<\/h3>\n<p><em>\u0423 \u043b\u044e\u0434\u0435\u0439 \u0435\u0441\u0442\u044c \u043a\u0430\u043a\u0438\u0435-\u0442\u043e \u043d\u0430\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u043e\u0441\u0442\u0438, \u0438 \u043d\u0430\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u043e\u0441\u0442\u0435\u0439 \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0443 \u043e\u0434\u043d\u043e\u0433\u043e \u0447\u0435\u043b\u043e\u0432\u0435\u043a\u0430. \u041e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c \u0432\u0441\u0435 \u043d\u0430\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u043e\u0441\u0442\u0438 \u0433\u0440\u0443\u043f\u043f\u044b \u043b\u044e\u0434\u0435\u0439, \u043f\u0440\u0438 \u0443\u0441\u043b\u043e\u0432\u0438\u0438 \u0442\u043e\u0433\u043e, \u0447\u0442\u043e \u043d\u0430\u0441 \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u0443\u044e\u0442 \u0442\u043e\u043b\u044c\u043a\u043e \u043a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u044b\u0435 \u043d\u0430\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u043e\u0441\u0442\u0438(\u043d\u0435 \u0432\u0441\u0435).<\/em><\/p>\n<p>\u0414\u0430\u043d\u043e:<\/p>\n<ol>\n<li>\n<p>\u041d\u0430\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u043e\u0441\u0442\u0438 \u043b\u044e\u0434\u0435\u0439. \u041f\u0440\u0438\u043c\u0435\u0440: <em>u1:{n1,n2,n3}, u2:{n1}, u4:{n3}, u5:{n2}, u6:{n1}<\/em><\/p>\n<\/li>\n<li>\n<p>\u041d\u0430\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u043e\u0441\u0442\u0438, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043d\u0430\u043c \u043d\u0443\u0436\u043d\u044b. \u041f\u0440\u0438\u043c\u0435\u0440: <em>cfgNs:{n1,n2,n5}<\/em><\/p>\n<\/li>\n<\/ol>\n<p>\u041d\u0430\u0439\u0442\u0438: <br \/>\u041d\u0430\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u043e\u0441\u0442\u0438 \u0433\u0440\u0443\u043f\u043f\u044b \u043b\u044e\u0434\u0435\u0439 <em>nationalities(group{u1,u2,u3,u4,u5})<\/em>. <br \/>\u041e\u0442\u0432\u0435\u0442 \u043d\u0430 \u043f\u0440\u0438\u043c\u0435\u0440: <em>{n1,n2}<\/em>.<\/p>\n<p>\u0420\u0435\u0448\u0435\u043d\u0438\u044f \u0431\u0443\u0434\u0443\u0442 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u044b \u043d\u0430 \u044f\u0437\u044b\u043a\u0435 Go.<\/p>\n<h3>\u0420\u0435\u0448\u0435\u043d\u0438\u044f<\/h3>\n<h4>1. \u0421\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u043e\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u0437\u0430\u0434\u0430\u0447\u0438<\/h4>\n<p>\u0414\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u0447\u0435\u043b\u043e\u0432\u0435\u043a\u0430 \u0438\u0437 \u0433\u0440\u0443\u043f\u043f\u044b \u043f\u043e \u0432\u0441\u0435\u043c \u043d\u0430\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u043e\u0441\u0442\u044f\u043c \u043f\u043e\u0432\u0435\u0440\u044f\u0435\u043c \u0432\u0445\u043e\u0436\u0434\u0435\u043d\u0438\u0435 \u0447\u0435\u043b\u043e\u0432\u0435\u043a\u0430 \u0432 \u0434\u0430\u043d\u043d\u0443\u044e \u043d\u0430\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u043e\u0441\u0442\u044c. \u0421\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c <em>O(n*<\/em>m).<\/p>\n<pre><code class=\"go\">type ( NationID uint64 PersonID uint64 )  type Calc struct { cfgNs    []NationID                         \/\/ \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u0443\u044e\u0449\u0438\u0435 \u043d\u0430\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u043e\u0441\u0442\u0438 n2people map[NationID]map[PersonID]struct{} \/\/ \u0441\u0432\u044f\u0437\u044c \u043d\u0430\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u043e\u0441\u0442\u044c -&gt; \u043b\u044e\u0434\u0438 }  func (c *Calc) Nationalities(group []PersonID) map[NationID]struct{} { res := make(map[NationID]struct{})  for _, n := range c.cfgNs { \/\/ \u0434\u043b\u044f \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u0443\u044e\u0449\u0438\u0445 \u043d\u0430\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u043e\u0441\u0442\u0435\u0439 nPeople, ok := c.n2people[n] \/\/ \u043b\u044e\u0434\u0438, \u0438\u043c\u0435\u044e\u0449\u0438\u0435 \u0434\u0430\u043d\u043d\u0443\u044e \u043d\u0430\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u043e\u0441\u0442\u044c if !ok { continue }  \/\/ \u0434\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u0447\u0435\u043b\u043e\u0432\u0435\u043a\u0430 \u0438\u0437 \u043a\u043e\u043c\u043f\u0430\u043d\u0438\u0438 for _, person := range group { \/\/ \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u0432 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442, \u0435\u0441\u043b\u0438 \u0447\u0435\u043b\u043e\u0432\u0435\u043a \u0435\u0441\u0442\u044c \u0432 \u044d\u0442\u043e\u0439 \u0433\u0440\u0443\u043f\u043f\u0435 if _, ok := nPeople[person]; ok { res[n] = struct{}{} break } } }  return res } <\/code><\/pre>\n<p>\u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u0431\u0435\u043d\u0447\u043c\u0430\u0440\u043a\u0430(Apple M1 Pro):<\/p>\n<pre><code class=\"bash\">1297513           918.9 ns\/op     520 B\/op       5 allocs\/op<\/code><\/pre>\n<p>\u041f\u043e\u043f\u0440\u043e\u0431\u0443\u0435\u043c \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c.<\/p>\n<h4>2. \u041e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u044f_1<\/h4>\n<p>\u041c\u043e\u0436\u043d\u043e \u0437\u0430\u043c\u0435\u0442\u0438\u0442\u044c, \u0447\u0442\u043e \u043d\u0430\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u043e\u0441\u0442\u0435\u0439, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043d\u0430\u043c \u043d\u0443\u0436\u043d\u044b, \u043d\u0435 \u0442\u0430\u043a \u043c\u043d\u043e\u0433\u043e (\u043e\u043a\u043e\u043b\u043e 30), \u0438 \u0442\u043e\u0433\u0434\u0430 \u043d\u0430\u0431\u043e\u0440 \u0433\u0440\u0443\u043f\u043f \u043c\u043e\u0436\u043d\u043e \u0437\u0430\u043c\u0435\u043d\u0438\u0442\u044c \u043d\u0430 \u0431\u0438\u0442\u043e\u0432\u0443\u044e \u043c\u0430\u0441\u043a\u0443.<\/p>\n<pre><code class=\"go\">type ( bitmask   uint64 NationIDs bitmask )  type CalcOptimized1 struct { cfgNs NationIDs              \/\/ \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u0443\u044e\u0449\u0438\u0435 \u043d\u0430\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u043e\u0441\u0442\u0438 p2ns  map[PersonID]NationIDs \/\/ \u0447\u0435\u043b\u043e\u0432\u0435\u043a -&gt; \u043d\u0430\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u043e\u0441\u0442\u0438 }  func (c *CalcOptimized1) Nationalities(group []PersonID) NationIDs { var res NationIDs  for _, person := range group { res |= c.p2ns[person] }  res &amp;= c.cfgNs  return res } <\/code><\/pre>\n<p>\u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u0431\u0435\u043d\u0447\u043c\u0430\u0440\u043a\u0430(Apple M1 Pro):<\/p>\n<pre><code class=\"bash\">13790236          86.22 ns\/op       0 B\/op       0 allocs\/op<\/code><\/pre>\n<p>\u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u0432\u0435\u0441\u044c\u043c\u0430 \u0438 \u0432\u0435\u0441\u044c\u043c\u0430 \u0445\u043e\u0440\u043e\u0448 (\u0432\u044b\u0438\u0433\u0440\u044b\u0448 \u0432 <strong>10.65<\/strong> \u0440\u0430\u0437). \u041d\u043e \u043c\u044b \u043f\u043e\u0439\u0434\u0435\u043c \u0434\u0430\u043b\u044c\u0448\u0435.<\/p>\n<h4>3. \u041e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u044f_2<\/h4>\n<p>\u041c\u043e\u0436\u043d\u043e \u0437\u0430\u043c\u0435\u0442\u0438\u0442\u044c, \u0447\u0442\u043e \u043c\u0430\u043f\u0430 \u0443 \u043d\u0430\u0441 \u0441\u043f\u0435\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u0430\u044f: <em>int -&gt; int<\/em>. \u0418 \u0442\u043e\u0433\u0434\u0430 \u0435\u0435 \u043c\u043e\u0436\u043d\u043e \u0437\u0430\u043c\u0435\u043d\u0438\u0442\u044c \u043d\u0430 \u0431\u044b\u0441\u0442\u0440\u0443\u044e \u0438\u043c\u043f\u043b\u0435\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u044e <em>intmap<\/em>.<\/p>\n<pre><code class=\"go\">import \"github.com\/kamstrup\/intmap\"  type CalcOptimized2 struct { cfgNs NationIDs                        \/\/ \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u0443\u044e\u0449\u0438\u0435 \u043d\u0430\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u043e\u0441\u0442\u0438 p2ns  *intmap.Map[PersonID, NationIDs] \/\/ \u0447\u0435\u043b\u043e\u0432\u0435\u043a -&gt; \u043d\u0430\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u043e\u0441\u0442\u0438 }  func (c *CalcOptimized2) Nationalities(group []PersonID) NationIDs { var res NationIDs  for _, person := range group { res |= mapGet(c.p2ns, person) }  res &amp;= c.cfgNs  return res } <\/code><\/pre>\n<p>\u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439(Apple M1 Pro):<\/p>\n<pre><code class=\"bash\">32768679          36.69 ns\/op       0 B\/op       0 allocs\/op<\/code><\/pre>\n<p>\u041f\u043e\u043b\u0443\u0447\u0438\u043b\u0438 \u0432\u044b\u0439\u0433\u0440\u044b\u0448 \u0435\u0449\u0435 \u0432 <strong>2.34<\/strong> \u0440\u0430\u0437\u0430 (\u0432 <strong>25 \u0440\u0430\u0437<\/strong> \u043e\u0442 \u043f\u0435\u0440\u0432\u043e\u043d\u0430\u0447\u0430\u043b\u044c\u043d\u043e\u0433\u043e). \u041f\u043e\u0439\u0434\u0435\u043c \u0434\u0430\u043b\u044c\u0448\u0435?<\/p>\n<h4>4. \u041e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u044f_3&#8242;<\/h4>\n<p>\u041c\u043e\u0436\u043d\u043e \u0437\u0430\u043c\u0435\u043d\u0438\u0442\u044c \u043c\u0430\u043f\u0443 \u043d\u0430 \u043c\u0430\u0441\u0441\u0438\u0432, \u0435\u0441\u043b\u0438 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435\u0439 \u043d\u0435 \u0442\u0430\u043a \u043c\u043d\u043e\u0433\u043e, \u043d\u043e \u0442\u043e\u0433\u0434\u0430 \u043f\u0430\u043c\u044f\u0442\u044c \u0431\u0443\u0434\u0435\u0442 \u0440\u0430\u0441\u0442\u0438 \u0441 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e\u043c \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435\u0439, \u0447\u0442\u043e \u043d\u0435 \u043e\u0447\u0435\u043d\u044c \u0445\u043e\u0440\u043e\u0448\u043e, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u044d\u0442\u0443 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u044e \u043b\u0443\u0447\u0448\u0435 \u043f\u0440\u0438\u043c\u0435\u043d\u044f\u0442\u044c \u0441 \u043e\u0441\u0442\u043e\u0440\u043e\u0436\u043d\u043e\u0441\u0442\u044c\u044e.<\/p>\n<pre><code class=\"go\">type CalcOptimized3Risky struct { cfgNs       NationIDs   \/\/ \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u0443\u044e\u0449\u0438\u0435 \u043d\u0430\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u043e\u0441\u0442\u0438 p2ns        []NationIDs \/\/ \u0447\u0435\u043b\u043e\u0432\u0435\u043a -&gt; \u043d\u0430\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u043e\u0441\u0442\u0438 maxPersonID PersonID }  func (c *CalcOptimized3Risky) Nationalities(group []PersonID) NationIDs { var res NationIDs  for _, person := range group { if person &lt;= c.maxPersonID { res |= c.p2ns[person] } }  res &amp;= c.cfgNs  return res } <\/code><\/pre>\n<p>\u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 (Apple M1 Pro):<\/p>\n<pre><code class=\"bash\">100000000         10.51 ns\/op       0 B\/op       0 allocs\/op<\/code><\/pre>\n<p>\u041f\u043e\u043b\u0443\u0447\u0438\u043b\u0438 \u0432\u044b\u0439\u0433\u0440\u044b\u0448 \u0435\u0449\u0435 \u0432 <strong>3.4<\/strong> \u0440\u0430\u0437\u0430 (\u0432\u044b\u0438\u0433\u0440\u044b\u0448 \u0432 <strong>87 \u0440\u0430\u0437<\/strong> \u043e\u0442 \u043f\u0435\u0440\u0432\u043e\u043d\u0430\u0447\u0430\u043b\u044c\u043d\u043e\u0433\u043e).<\/p>\n<p>\u041d\u0430 \u044d\u0442\u043e\u043c \u0432\u0441\u0435!<\/p>\n<h3>Benchmark Results<\/h3>\n<pre><code class=\"bash\">goos: darwin goarch: arm64 pkg: github.com\/ilyadt\/benchmap cpu: Apple M1 Pro BenchmarkCalculators\/calculator-8                     918.9 ns\/op BenchmarkCalculators\/calculator_optimized1-8           86.2 ns\/op BenchmarkCalculators\/calculator_optimized2-8           36.6 ns\/op BenchmarkCalculators\/calculator_optimized3_risky-8     10.5 ns\/op<\/code><\/pre>\n<h3>P.S.<\/h3>\n<p>\u041f\u0440\u043e\u0431\u043e\u0432\u0430\u043b \u0442\u0430\u043a \u0436\u0435 \u0437\u0430\u043c\u0435\u043d\u044f\u0442\u044c \u043c\u0430\u043f\u0443 \u043d\u0430 <em>RoaringBitmap<\/em> \u043f\u043e \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f\u043c (n \u2192 rb{people}), \u043d\u043e \u043f\u043e \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438 \u043e\u043d\u0430 \u043f\u0440\u043e\u0438\u0433\u0440\u044b\u0432\u0430\u0435\u0442 \u0434\u043b\u044f \u044d\u0442\u043e\u0439 \u043a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u043e\u0439 \u0437\u0430\u0434\u0430\u0447\u0438.<\/p>\n<h3>\u0418\u0442\u043e\u0433<\/h3>\n<ol>\n<li>\n<p>\u0425\u043e\u0442\u044c \u043c\u0430\u043f\u0430 \u0438 \u0431\u044b\u0441\u0442\u0440\u0430\u044f (\u043f\u043e\u0438\u0441\u043a \u043f\u043e \u043a\u043b\u044e\u0447\u0443 O(1)), \u043d\u043e \u043f\u043e \u0444\u0430\u043a\u0442\u0443 \u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f \u0432\u0440\u0435\u043c\u044f \u043c\u043d\u043e\u0433\u043e \u0431\u043e\u043b\u044c\u0448\u0435, \u0447\u0435\u043c \u0432 \u043f\u043e\u0438\u0441\u043a\u0435 \u043f\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0443, \u0433\u0434\u0435 \u0442\u043e\u0436\u0435 O(1). \u041e\u0441\u043e\u0431\u0435\u043d\u043d\u043e \u044d\u0442\u043e \u0437\u0430\u043c\u0435\u0442\u043d\u043e \u0432 \u0446\u0438\u043a\u043b\u0430\u0445.<\/p>\n<\/li>\n<li>\n<p>\u041f\u0440\u0438 \u043c\u0430\u043b\u043e\u043c \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u0435 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u044b\u0445 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439 \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c bitset. \u0427\u0442\u043e \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c \u043f\u0440\u0438\u043d\u0430\u0434\u043b\u0435\u0436\u043d\u043e\u0441\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0443 \u0437\u0430 \u043e\u0434\u043d\u0443 \u043f\u043e\u0431\u0438\u0442\u043e\u0432\u0443\u044e \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044e.<\/p>\n<\/li>\n<li>\n<p>\u0415\u0441\u043b\u0438 \u043c\u0430\u043f\u0430 \u0441\u043f\u0435\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u0430\u044f (\u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 int-&gt;int), \u0442\u043e \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0441\u043f\u0435\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0435 \u0438\u043c\u043b\u0435\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u0438 \u0432\u043c\u0435\u0441\u0442\u043e \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u043e\u0439 \u043c\u0430\u043f\u044b. \u0412 \u043c\u043e\u0435\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u044d\u0442\u043e \u0443\u0432\u0435\u043b\u0438\u0447\u0438\u043b\u043e \u0441\u043a\u043e\u0440\u043e\u0441\u0442\u044c \u0432 <strong>2.29<\/strong> \u0440\u0430\u0437\u0430.<\/p>\n<\/li>\n<\/ol>\n<h3>\u0421\u0441\u044b\u043b\u043a\u0438<\/h3>\n<ul>\n<li>\n<p>\u0411\u0435\u043d\u0447\u043c\u0430\u0440\u043a\u0438 \u2014 <a href=\"https:\/\/github.com\/ilyadt\/benchmap\" rel=\"noopener noreferrer nofollow\">https:\/\/github.com\/ilyadt\/benchmap<\/a><\/p>\n<\/li>\n<li>\n<p>\u0420\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u043e\u0439 intmap \u2014 <a href=\"https:\/\/github.com\/kamstrup\/intmap\" rel=\"noopener noreferrer nofollow\">https:\/\/github.com\/kamstrup\/intmap<\/a><\/p>\n<\/li>\n<li>\n<p>\u0414\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u043e: RoaringBitmap \u2014 <a href=\"https:\/\/github.com\/RoaringBitmap\/roaring\" rel=\"noopener noreferrer nofollow\">https:\/\/github.com\/RoaringBitmap\/roaring<\/a><\/p>\n<\/li>\n<\/ul>\n<\/div>\n<\/div>\n<\/div>\n<p><!----><!----><\/div>\n<p><!----><!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/articles\/901404\/\"> https:\/\/habr.com\/ru\/articles\/901404\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<h3>\u0412\u0432\u0435\u0434\u0435\u043d\u0438\u0435<\/h3>\n<p><strong>\u0425\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0430(\u043c\u0430\u043f\u0430)<\/strong> \u2014 \u043e\u0434\u043d\u0430 \u0438\u0437 \u0441\u0430\u043c\u044b\u0445 \u043f\u043e\u043f\u0443\u043b\u044f\u0440\u043d\u044b\u0445 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440 \u0434\u0430\u043d\u043d\u044b\u0445, \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e \u043f\u043e\u0438\u0441\u043a \u043f\u043e \u043a\u043b\u044e\u0447\u0443 \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442 \u0437\u0430 <strong>O(1)<\/strong>. \u041f\u0440\u0438\u0447\u0435\u043c \u043a\u043b\u044e\u0447 \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u043b\u044e\u0431\u044b\u043c \u043b\u044e\u0431\u044b\u043c \u0442\u0438\u043f\u043e\u043c, \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043a\u043e\u0442\u043e\u0440\u043e\u0433\u043e \u043c\u043e\u0436\u043d\u043e \u0441\u0440\u0430\u0432\u043d\u0438\u0432\u0430\u0442\u044c (Comparable Trait).<\/p>\n<p>\u042f \u0441\u0442\u043e\u043b\u043a\u043d\u0443\u043b\u0441\u044f \u0441 \u0442\u0435\u043c, \u0447\u0442\u043e \u043c\u0430\u043f\u0430 \u043d\u0435 \u0442\u0430\u043a\u0430\u044f \u0431\u044b\u0441\u0442\u0440\u0430\u044f \u043f\u043e \u0431\u0435\u043d\u0447\u043c\u0430\u0440\u043a\u0430\u043c \u043d\u0430 \u044f\u0437\u044b\u043a\u0435 GO, \u0445\u043e\u0442\u044f \u0442\u0435\u043e\u0440\u0435\u0442\u0438\u0447\u0435\u0441\u043a\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430 \u041e(1).<\/p>\n<p>\u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0443\u044e \u0437\u0430\u0434\u0430\u0447\u0443 \u0438 \u0441\u043f\u043e\u0441\u043e\u0431\u044b \u0435\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u044f.<\/p>\n<h3>\u0417\u0430\u0434\u0430\u0447\u0430<\/h3>\n<p><em>\u0423 \u043b\u044e\u0434\u0435\u0439 \u0435\u0441\u0442\u044c \u043a\u0430\u043a\u0438\u0435-\u0442\u043e \u043d\u0430\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u043e\u0441\u0442\u0438, \u0438 \u043d\u0430\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u043e\u0441\u0442\u0435\u0439 \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0443 \u043e\u0434\u043d\u043e\u0433\u043e \u0447\u0435\u043b\u043e\u0432\u0435\u043a\u0430. \u041e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c \u0432\u0441\u0435 \u043d\u0430\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u043e\u0441\u0442\u0438 \u0433\u0440\u0443\u043f\u043f\u044b \u043b\u044e\u0434\u0435\u0439, \u043f\u0440\u0438 \u0443\u0441\u043b\u043e\u0432\u0438\u0438 \u0442\u043e\u0433\u043e, \u0447\u0442\u043e \u043d\u0430\u0441 \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u0443\u044e\u0442 \u0442\u043e\u043b\u044c\u043a\u043e \u043a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u044b\u0435 \u043d\u0430\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u043e\u0441\u0442\u0438(\u043d\u0435 \u0432\u0441\u0435).<\/em><\/p>\n<p>\u0414\u0430\u043d\u043e:<\/p>\n<ol>\n<li>\n<p>\u041d\u0430\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u043e\u0441\u0442\u0438 \u043b\u044e\u0434\u0435\u0439. \u041f\u0440\u0438\u043c\u0435\u0440: <em>u1:{n1,n2,n3}, u2:{n1}, u4:{n3}, u5:{n2}, u6:{n1}<\/em><\/p>\n<\/li>\n<li>\n<p>\u041d\u0430\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u043e\u0441\u0442\u0438, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043d\u0430\u043c \u043d\u0443\u0436\u043d\u044b. \u041f\u0440\u0438\u043c\u0435\u0440: <em>cfgNs:{n1,n2,n5}<\/em><\/p>\n<\/li>\n<\/ol>\n<p>\u041d\u0430\u0439\u0442\u0438: <br \/>\u041d\u0430\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u043e\u0441\u0442\u0438 \u0433\u0440\u0443\u043f\u043f\u044b \u043b\u044e\u0434\u0435\u0439 <em>nationalities(group{u1,u2,u3,u4,u5})<\/em>. <br \/>\u041e\u0442\u0432\u0435\u0442 \u043d\u0430 \u043f\u0440\u0438\u043c\u0435\u0440: <em>{n1,n2}<\/em>.<\/p>\n<p>\u0420\u0435\u0448\u0435\u043d\u0438\u044f \u0431\u0443\u0434\u0443\u0442 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u044b \u043d\u0430 \u044f\u0437\u044b\u043a\u0435 Go.<\/p>\n<h3>\u0420\u0435\u0448\u0435\u043d\u0438\u044f<\/h3>\n<h4>1. \u0421\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u043e\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u0437\u0430\u0434\u0430\u0447\u0438<\/h4>\n<p>\u0414\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u0447\u0435\u043b\u043e\u0432\u0435\u043a\u0430 \u0438\u0437 \u0433\u0440\u0443\u043f\u043f\u044b \u043f\u043e \u0432\u0441\u0435\u043c \u043d\u0430\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u043e\u0441\u0442\u044f\u043c \u043f\u043e\u0432\u0435\u0440\u044f\u0435\u043c \u0432\u0445\u043e\u0436\u0434\u0435\u043d\u0438\u0435 \u0447\u0435\u043b\u043e\u0432\u0435\u043a\u0430 \u0432 \u0434\u0430\u043d\u043d\u0443\u044e \u043d\u0430\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u043e\u0441\u0442\u044c. \u0421\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c <em>O(n*<\/em>m).<\/p>\n<pre><code class=\"go\">type ( NationID uint64 PersonID uint64 )  type Calc struct { cfgNs    []NationID                         \/\/ \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u0443\u044e\u0449\u0438\u0435 \u043d\u0430\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u043e\u0441\u0442\u0438 n2people map[NationID]map[PersonID]struct{} \/\/ \u0441\u0432\u044f\u0437\u044c \u043d\u0430\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u043e\u0441\u0442\u044c -&gt; \u043b\u044e\u0434\u0438 }  func (c *Calc) Nationalities(group []PersonID) map[NationID]struct{} { res := make(map[NationID]struct{})  for _, n := range c.cfgNs { \/\/ \u0434\u043b\u044f \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u0443\u044e\u0449\u0438\u0445 \u043d\u0430\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u043e\u0441\u0442\u0435\u0439 nPeople, ok := c.n2people[n] \/\/ \u043b\u044e\u0434\u0438, \u0438\u043c\u0435\u044e\u0449\u0438\u0435 \u0434\u0430\u043d\u043d\u0443\u044e \u043d\u0430\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u043e\u0441\u0442\u044c if !ok { continue }  \/\/ \u0434\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u0447\u0435\u043b\u043e\u0432\u0435\u043a\u0430 \u0438\u0437 \u043a\u043e\u043c\u043f\u0430\u043d\u0438\u0438 for _, person := range group { \/\/ \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u0432 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442, \u0435\u0441\u043b\u0438 \u0447\u0435\u043b\u043e\u0432\u0435\u043a \u0435\u0441\u0442\u044c \u0432 \u044d\u0442\u043e\u0439 \u0433\u0440\u0443\u043f\u043f\u0435 if _, ok := nPeople[person]; ok { res[n] = struct{}{} break } } }  return res } <\/code><\/pre>\n<p>\u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u0431\u0435\u043d\u0447\u043c\u0430\u0440\u043a\u0430(Apple M1 Pro):<\/p>\n<pre><code class=\"bash\">1297513           918.9 ns\/op     520 B\/op       5 allocs\/op<\/code><\/pre>\n<p>\u041f\u043e\u043f\u0440\u043e\u0431\u0443\u0435\u043c \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c.<\/p>\n<h4>2. \u041e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u044f_1<\/h4>\n<p>\u041c\u043e\u0436\u043d\u043e \u0437\u0430\u043c\u0435\u0442\u0438\u0442\u044c, \u0447\u0442\u043e \u043d\u0430\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u043e\u0441\u0442\u0435\u0439, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043d\u0430\u043c \u043d\u0443\u0436\u043d\u044b, \u043d\u0435 \u0442\u0430\u043a \u043c\u043d\u043e\u0433\u043e (\u043e\u043a\u043e\u043b\u043e 30), \u0438 \u0442\u043e\u0433\u0434\u0430 \u043d\u0430\u0431\u043e\u0440 \u0433\u0440\u0443\u043f\u043f \u043c\u043e\u0436\u043d\u043e \u0437\u0430\u043c\u0435\u043d\u0438\u0442\u044c \u043d\u0430 \u0431\u0438\u0442\u043e\u0432\u0443\u044e \u043c\u0430\u0441\u043a\u0443.<\/p>\n<pre><code class=\"go\">type ( bitmask   uint64 NationIDs bitmask )  type CalcOptimized1 struct { cfgNs NationIDs              \/\/ \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u0443\u044e\u0449\u0438\u0435 \u043d\u0430\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u043e\u0441\u0442\u0438 p2ns  map[PersonID]NationIDs \/\/ \u0447\u0435\u043b\u043e\u0432\u0435\u043a -&gt; \u043d\u0430\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u043e\u0441\u0442\u0438 }  func (c *CalcOptimized1) Nationalities(group []PersonID) NationIDs { var res NationIDs  for _, person := range group { res |= c.p2ns[person] }  res &amp;= c.cfgNs  return res } <\/code><\/pre>\n<p>\u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u0431\u0435\u043d\u0447\u043c\u0430\u0440\u043a\u0430(Apple M1 Pro):<\/p>\n<pre><code class=\"bash\">13790236          86.22 ns\/op       0 B\/op       0 allocs\/op<\/code><\/pre>\n<p>\u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u0432\u0435\u0441\u044c\u043c\u0430 \u0438 \u0432\u0435\u0441\u044c\u043c\u0430 \u0445\u043e\u0440\u043e\u0448 (\u0432\u044b\u0438\u0433\u0440\u044b\u0448 \u0432 <strong>10.65<\/strong> \u0440\u0430\u0437). \u041d\u043e \u043c\u044b \u043f\u043e\u0439\u0434\u0435\u043c \u0434\u0430\u043b\u044c\u0448\u0435.<\/p>\n<h4>3. \u041e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u044f_2<\/h4>\n<p>\u041c\u043e\u0436\u043d\u043e \u0437\u0430\u043c\u0435\u0442\u0438\u0442\u044c, \u0447\u0442\u043e \u043c\u0430\u043f\u0430 \u0443 \u043d\u0430\u0441 \u0441\u043f\u0435\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u0430\u044f: <em>int -&gt; int<\/em>. \u0418 \u0442\u043e\u0433\u0434\u0430 \u0435\u0435 \u043c\u043e\u0436\u043d\u043e \u0437\u0430\u043c\u0435\u043d\u0438\u0442\u044c \u043d\u0430 \u0431\u044b\u0441\u0442\u0440\u0443\u044e \u0438\u043c\u043f\u043b\u0435\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u044e <em>intmap<\/em>.<\/p>\n<pre><code class=\"go\">import \"github.com\/kamstrup\/intmap\"  type CalcOptimized2 struct { cfgNs NationIDs                        \/\/ \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u0443\u044e\u0449\u0438\u0435 \u043d\u0430\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u043e\u0441\u0442\u0438 p2ns  *intmap.Map[PersonID, NationIDs] \/\/ \u0447\u0435\u043b\u043e\u0432\u0435\u043a -&gt; \u043d\u0430\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u043e\u0441\u0442\u0438 }  func (c *CalcOptimized2) Nationalities(group []PersonID) NationIDs { var res NationIDs  for _, person := range group { res |= mapGet(c.p2ns, person) }  res &amp;= c.cfgNs  return res } <\/code><\/pre>\n<p>\u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439(Apple M1 Pro):<\/p>\n<pre><code class=\"bash\">32768679          36.69 ns\/op       0 B\/op       0 allocs\/op<\/code><\/pre>\n<p>\u041f\u043e\u043b\u0443\u0447\u0438\u043b\u0438 \u0432\u044b\u0439\u0433\u0440\u044b\u0448 \u0435\u0449\u0435 \u0432 <strong>2.34<\/strong> \u0440\u0430\u0437\u0430 (\u0432 <strong>25 \u0440\u0430\u0437<\/strong> \u043e\u0442 \u043f\u0435\u0440\u0432\u043e\u043d\u0430\u0447\u0430\u043b\u044c\u043d\u043e\u0433\u043e). \u041f\u043e\u0439\u0434\u0435\u043c \u0434\u0430\u043b\u044c\u0448\u0435?<\/p>\n<h4>4. \u041e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u044f_3&#8242;<\/h4>\n<p>\u041c\u043e\u0436\u043d\u043e \u0437\u0430\u043c\u0435\u043d\u0438\u0442\u044c \u043c\u0430\u043f\u0443 \u043d\u0430 \u043c\u0430\u0441\u0441\u0438\u0432, \u0435\u0441\u043b\u0438 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435\u0439 \u043d\u0435 \u0442\u0430\u043a \u043c\u043d\u043e\u0433\u043e, \u043d\u043e \u0442\u043e\u0433\u0434\u0430 \u043f\u0430\u043c\u044f\u0442\u044c \u0431\u0443\u0434\u0435\u0442 \u0440\u0430\u0441\u0442\u0438 \u0441 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e\u043c \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435\u0439, \u0447\u0442\u043e \u043d\u0435 \u043e\u0447\u0435\u043d\u044c \u0445\u043e\u0440\u043e\u0448\u043e, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u044d\u0442\u0443 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u044e \u043b\u0443\u0447\u0448\u0435 \u043f\u0440\u0438\u043c\u0435\u043d\u044f\u0442\u044c \u0441 \u043e\u0441\u0442\u043e\u0440\u043e\u0436\u043d\u043e\u0441\u0442\u044c\u044e.<\/p>\n<pre><code class=\"go\">type CalcOptimized3Risky struct { cfgNs       NationIDs   \/\/ \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u0443\u044e\u0449\u0438\u0435 \u043d\u0430\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u043e\u0441\u0442\u0438 p2ns        []NationIDs \/\/ \u0447\u0435\u043b\u043e\u0432\u0435\u043a -&gt; \u043d\u0430\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u043e\u0441\u0442\u0438 maxPersonID PersonID }  func (c *CalcOptimized3Risky) Nationalities(group []PersonID) NationIDs { var res NationIDs  for _, person := range group { if person &lt;= c.maxPersonID { res |= c.p2ns[person] } }  res &amp;= c.cfgNs  return res } <\/code><\/pre>\n<p>\u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 (Apple M1 Pro):<\/p>\n<pre><code class=\"bash\">100000000         10.51 ns\/op       0 B\/op       0 allocs\/op<\/code><\/pre>\n<p>\u041f\u043e\u043b\u0443\u0447\u0438\u043b\u0438 \u0432\u044b\u0439\u0433\u0440\u044b\u0448 \u0435\u0449\u0435 \u0432 <strong>3.4<\/strong> \u0440\u0430\u0437\u0430 (\u0432\u044b\u0438\u0433\u0440\u044b\u0448 \u0432 <strong>87 \u0440\u0430\u0437<\/strong> \u043e\u0442 \u043f\u0435\u0440\u0432\u043e\u043d\u0430\u0447\u0430\u043b\u044c\u043d\u043e\u0433\u043e).<\/p>\n<p>\u041d\u0430 \u044d\u0442\u043e\u043c \u0432\u0441\u0435!<\/p>\n<h3>Benchmark Results<\/h3>\n<pre><code class=\"bash\">goos: darwin goarch: arm64 pkg: github.com\/ilyadt\/benchmap cpu: Apple M1 Pro BenchmarkCalculators\/calculator-8                     918.9 ns\/op BenchmarkCalculators\/calculator_optimized1-8           86.2 ns\/op BenchmarkCalculators\/calculator_optimized2-8           36.6 ns\/op BenchmarkCalculators\/calculator_optimized3_risky-8     10.5 ns\/op<\/code><\/pre>\n<h3>P.S.<\/h3>\n<p>\u041f\u0440\u043e\u0431\u043e\u0432\u0430\u043b \u0442\u0430\u043a \u0436\u0435 \u0437\u0430\u043c\u0435\u043d\u044f\u0442\u044c \u043c\u0430\u043f\u0443 \u043d\u0430 <em>RoaringBitmap<\/em> \u043f\u043e \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f\u043c (n \u2192 rb{people}), \u043d\u043e \u043f\u043e \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438 \u043e\u043d\u0430 \u043f\u0440\u043e\u0438\u0433\u0440\u044b\u0432\u0430\u0435\u0442 \u0434\u043b\u044f \u044d\u0442\u043e\u0439 \u043a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u043e\u0439 \u0437\u0430\u0434\u0430\u0447\u0438.<\/p>\n<h3>\u0418\u0442\u043e\u0433<\/h3>\n<ol>\n<li>\n<p>\u0425\u043e\u0442\u044c \u043c\u0430\u043f\u0430 \u0438 \u0431\u044b\u0441\u0442\u0440\u0430\u044f (\u043f\u043e\u0438\u0441\u043a \u043f\u043e \u043a\u043b\u044e\u0447\u0443 O(1)), \u043d\u043e \u043f\u043e \u0444\u0430\u043a\u0442\u0443 \u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f \u0432\u0440\u0435\u043c\u044f \u043c\u043d\u043e\u0433\u043e \u0431\u043e\u043b\u044c\u0448\u0435, \u0447\u0435\u043c \u0432 \u043f\u043e\u0438\u0441\u043a\u0435 \u043f\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0443, \u0433\u0434\u0435 \u0442\u043e\u0436\u0435 O(1). \u041e\u0441\u043e\u0431\u0435\u043d\u043d\u043e \u044d\u0442\u043e \u0437\u0430\u043c\u0435\u0442\u043d\u043e \u0432 \u0446\u0438\u043a\u043b\u0430\u0445.<\/p>\n<\/li>\n<li>\n<p>\u041f\u0440\u0438 \u043c\u0430\u043b\u043e\u043c \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u0435 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u044b\u0445 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439 \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c bitset. \u0427\u0442\u043e \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c \u043f\u0440\u0438\u043d\u0430\u0434\u043b\u0435\u0436\u043d\u043e\u0441\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0443 \u0437\u0430 \u043e\u0434\u043d\u0443 \u043f\u043e\u0431\u0438\u0442\u043e\u0432\u0443\u044e \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044e.<\/p>\n<\/li>\n<li>\n<p>\u0415\u0441\u043b\u0438 \u043c\u0430\u043f\u0430 \u0441\u043f\u0435\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u0430\u044f (\u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 int-&gt;int), \u0442\u043e \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0441\u043f\u0435\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0435 \u0438\u043c\u043b\u0435\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u0438 \u0432\u043c\u0435\u0441\u0442\u043e \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u043e\u0439 \u043c\u0430\u043f\u044b. \u0412 \u043c\u043e\u0435\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u044d\u0442\u043e \u0443\u0432\u0435\u043b\u0438\u0447\u0438\u043b\u043e \u0441\u043a\u043e\u0440\u043e\u0441\u0442\u044c \u0432 <strong>2.29<\/strong> \u0440\u0430\u0437\u0430.<\/p>\n<\/li>\n<\/ol>\n<h3>\u0421\u0441\u044b\u043b\u043a\u0438<\/h3>\n<ul>\n<li>\n<p>\u0411\u0435\u043d\u0447\u043c\u0430\u0440\u043a\u0438 \u2014 <a href=\"https:\/\/github.com\/ilyadt\/benchmap\" rel=\"noopener noreferrer nofollow\">https:\/\/github.com\/ilyadt\/benchmap<\/a><\/p>\n<\/li>\n<li>\n<p>\u0420\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u043e\u0439 intmap \u2014 <a href=\"https:\/\/github.com\/kamstrup\/intmap\" rel=\"noopener noreferrer nofollow\">https:\/\/github.com\/kamstrup\/intmap<\/a><\/p>\n<\/li>\n<li>\n<p>\u0414\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u043e: RoaringBitmap \u2014 <a href=\"https:\/\/github.com\/RoaringBitmap\/roaring\" rel=\"noopener noreferrer nofollow\">https:\/\/github.com\/RoaringBitmap\/roaring<\/a><\/p>\n<\/li>\n<\/ul>\n<\/div>\n<\/div>\n<\/div>\n<p><!----><!----><\/div>\n<p><!----><!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/articles\/901404\/\"> https:\/\/habr.com\/ru\/articles\/901404\/<\/a><br \/><\/br><\/br><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-456296","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/456296","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=456296"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/456296\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=456296"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=456296"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=456296"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}