<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:blogger='http://schemas.google.com/blogger/2008' xmlns:georss='http://www.georss.org/georss' xmlns:gd="http://schemas.google.com/g/2005" xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-1806360094658697411</id><updated>2025-08-27T00:50:49.984-04:00</updated><title type='text'>The Lowly Programmer</title><subtitle type='html'>Musings in code</subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://www.thelowlyprogrammer.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1806360094658697411/posts/default?redirect=false'/><link rel='alternate' type='text/html' href='http://www.thelowlyprogrammer.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><link rel='next' type='application/atom+xml' href='http://www.blogger.com/feeds/1806360094658697411/posts/default?start-index=26&amp;max-results=25&amp;redirect=false'/><author><name>Eric Burnett</name><uri>http://www.blogger.com/profile/10741882872804697111</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>28</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-1806360094658697411.post-7774054165909597425</id><published>2021-04-26T01:04:00.001-04:00</published><updated>2021-04-26T01:04:12.156-04:00</updated><title type='text'>The Rise of Datacenter Computing</title><content type='html'>&lt;p&gt;The coming decades will see a significant change in the usage and deployment of “compute” (herein CPU cycles, storage, etc), specifically relating to the rise of datacenter computing. I don’t
    know the exact form this change will take - though I will point to the first
    phases of it below - but the fact that significant paradigm shifts are
    coming seems inevitable.&lt;/p&gt;
&lt;p&gt;Note that I’m not talking about IoT or anything in that space, though
      it’s peripherally related. Here I’m specifically talking about the rise of
  datacenters as the majority share of compute &lt;i&gt;power&lt;/i&gt;, and how the economy of computing and our individual access will be reshaped by
      it.&lt;/p&gt;
&lt;h2&gt;Part 1: The Data Center of Gravity&lt;/h2&gt;
&lt;p&gt;Two trends here seem clear: first, the balance of compute power is going
      to continue shifting towards datacenters and away from personal devices
      (even as those devices proliferate); and second, economics will dictate a
      shift in how this capacity is delivered to meet the ever-growing
      demand.&lt;/p&gt;
&lt;p&gt;The balance of compute power will shift inexorably &lt;b&gt;towards Centralized&lt;/b&gt;: as the fraction of GDP &lt;a
      href=&quot;https://www.fortunebusinessinsights.com/semiconductor-market-102365&quot;&gt;put towards compute&lt;/a&gt;
      rises, the portion of it situated in the home and on the person will fall,
      as will non-datacenter corporate compute.&lt;/p&gt;
  &lt;ol&gt;
    &lt;li&gt;&lt;p&gt;Centralized is efficient: better power utilization (&lt;a
          href=&quot;https://en.wikipedia.org/wiki/Power_usage_effectiveness&quot;&gt;PUE&lt;/a&gt;), better resource utilization, more cost-efficient devices (e.g.
          larger processors, rack-oriented computer designs) all favour
          datacenter compute. But also because high density computing itself
          often allows for more efficient processing: lots of computers and
          memory and storage together on a high bandwidth fabric permits
          algorithms ill-suited to - or impossible on - highly-distributed
          small-scale devices.
    &lt;/li&gt;
    &lt;li&gt;&lt;p&gt;Our willingness to have compute resources poorly utilized and at high
          risk will decline. Most compute capability of a cell phone or a
          personal computer goes almost entirely wasted already, which is
          tolerable for the convenience they bring at the relative cost. But
          would you want a $100k computer sitting idle at home, and a $10k cell
          phone in your pocket, if that’s what it took to keep up with compute
          demand growth? Almost certainly not. And relatedly, we’re seeing a
          rapid shift from low efficiency corporate computing - on-prem, colo,
          and private small datacenters - towards larger datacenters and
          especially &lt;a
          href=&quot;https://www.digitalrealty.com/blog/what-is-hyperscale&quot;&gt;hyperscalars&lt;/a&gt;
          with cost effectiveness as one of the driving motivators.
    &lt;/li&gt;
    &lt;li&gt;&lt;p&gt;When we consider the electricity consumption of global compute rising
          from the present-day ~3% (including consumer devices, datacenters, and
          networks) to say &lt;a
          href=&quot;https://www.nature.com/articles/d41586-018-06610-y&quot;&gt;21% by 2030&lt;/a&gt;
          (or perhaps a few years later once &lt;a
          href=&quot;https://www.wired.com/story/data-centers-not-devouring-planet-electricity-yet/&quot;&gt;efficiencies are played out&lt;/a&gt;), we couldn’t carry all those watt-hours in our collective pocket
          even if we wanted to. At best it could shift to remote devices that
          live in the home, but efficiency still pushes strongly for it being
          situated near cheap electricity instead.
    &lt;/li&gt;
  &lt;/ol&gt;
  &lt;p&gt;This is not to say that individuals won’t continue to grow their net
      compute usage. Rather, the trend of &lt;i&gt;comparatively&lt;/i&gt;
      weak computing devices on the person and in the home (e.g. 2-4 core cell
      phones) will continue, augmented by a growing fraction of compute
      happening in responsibilities offloaded to centralized (datacenter)
      environments and their hundred-plus core machines.&lt;/p&gt;
  &lt;p&gt;To make this concrete: today the majority of compute capabilities (e.g.
      CPU cores) remain in the hands of consumers, in the form of cell phones,
      tablets, laptops, PCs, game consoles, and other such devices. But
      server-oriented compute devices are rapidly growing, with &lt;a
      href=&quot;https://www.forbes.com/sites/tomcoughlin/2020/05/29/hdd-market-history-and-projections/?sh=7f50485c6682&quot;&gt;disks&lt;/a&gt;
      leading the charge. Note that server-oriented disks do not dominate by
      unit &lt;i&gt;count&lt;/i&gt; yet, but already do by &lt;i&gt;capacity&lt;/i&gt;
      as the server- and consumer-oriented devices diverge. &lt;a
      href=&quot;https://www.investopedia.com/how-nvidia-makes-money-4799532&quot;&gt;GPUs&lt;/a&gt;
      are soon to tip towards server-dominant as well (and may have already when
      measured by capabilities, if we account for GPUs used crypto mining that
      appear in the consumer “Graphics” segment, capability segmentation
      consumer vs enterprise, and generally higher prices / revenue in the
      consumer market skewing revenue-based breakdowns). CPUs and ram are the
      most difficult to pinpoint, but anecdotally servers helping to &lt;a
      href=&quot;https://seekingalpha.com/article/4346547-micron-samsung-and-sk-hynix-dram-oligopoly&quot;&gt;hold up the ram market&lt;/a&gt;, and AMD’s &lt;a
      href=&quot;https://www.forbes.com/sites/moorinsights/2021/03/24/amd-will-make-significant-gains-in-the-enterprise-with-milan/?sh=1dcb67ba3eb9&quot;&gt;datacenter-first strategy&lt;/a&gt;, suggest that we’re nearing the tipping point there too.&lt;/p&gt;
&lt;p&gt;By &lt;i&gt;usage &lt;/i&gt;aka &lt;i&gt;utilization&lt;/i&gt;, datacenters are already likely dominant, and growing rapidly. The
      average consumer device has an extremely low overall utilization
      (single-digit percentage), when considering the time-on-device, core
      scaling, and frequency scaling utilized by devices to keep power
      consumption low. Whereas datacenters have entire teams dedicated to
      keeping utilization high and timing capacity delivery against demand,
      achieving overall utilizations reaching 50% and likely averaging 30-40%
      industry-wide. Combined with the relative share of compute capabilities,
      this suggests the balance of computing is &lt;i&gt;already&lt;/i&gt;
      happening in datacenters, and incremental compute coming online suggests
      usage highly skewed towards datacenters.&lt;/p&gt;
  &lt;p&gt;On the economic side, the complexity of producing these datacenters will
      continue to shift as well. &lt;b&gt;“Supply” will tend towards fixed&lt;/b&gt; 
      over the coming decades: not deployed according to short-term needs, but
      rather, at a fixed pace determined over increasingly long time-scales.
      Consider the recurring semiconductor shortages (bottlenecked on
      fabrication capacity) combined with the rising price of chip fabs, the
      lead times on electric grid scaling, the lead time on adjusting mining
      output for rare earth elements, the cost of a modern datacenter building,
      and the general inertia of any industry as its share of global GDP
      rises.&lt;/p&gt;
  &lt;p&gt;This will see datacenter compute capabilities continue the shift into
      “base infrastructure” IaaS / PaaS plus higher levels layered above, with
      Public Clouds being only the first step on this road. One might also
      expect planning to trend towards more centralized as well: akin to the Oil
      industry, I could imagine the producers and consumers of bottleneck
      infrastructure resources - power, computer chips, flash, and disk drives
      today - coming together to commit to a particular curve years or decades
      in advance, and pre-committing demand in a way that makes the funding of
      mega fabs (&lt;a
      href=&quot;https://www.taiwannews.com.tw/en/news/3805032&quot;&gt;$20B&lt;/a&gt;
      today; $50B in a couple more generations) possible.&lt;/p&gt;
  &lt;p&gt;Beyond that, I’m not sure. Do whole countries start to take a seat at the
      negotiating table? The world’s reliance on TSMC for top-end chips, and the
      collaboration with the US government to land a fab on US soil, suggests
      it’s decently likely. Does the cryptocurrency share get reigned in? I
      guess we’ll see.&lt;/p&gt;
  &lt;h2&gt;Part 2: “Personal” computing&lt;/h2&gt;
  &lt;p&gt;Let’s run a thought experiment: what does it look like for centralized
      “compute” to become available not only to corporations who pay for it, but
      also made directly and practically available to the citizens of the world.
      What will it be used for? How will that be decided?&lt;/p&gt;
  &lt;p&gt;“Practically” is important - it’s true that individuals can sign up for
      accounts at public Clouds today, and use the many services available, but
      the vast majority do not have the knowledge necessary to do so
      successfully. Whereas almost everyone knows how to download and run an app
      on a local device. As such, the predominant consumer compute patterns
      leveraged today are:&lt;/p&gt;
  &lt;ol&gt;
    &lt;li&gt;&lt;p&gt;“&lt;i&gt;Local general-purpose&lt;/i&gt;”: apps and programs running on the capabilities of a device you
          provide.
    &lt;/li&gt;
    &lt;li&gt;&lt;p&gt;“&lt;i&gt;Ad-supported&lt;/i&gt;”: websites, social networks, etc, where the load you personally
          contribute is small enough to be funded entirely by advertising or
          offered for free; requiring no payments either way.
    &lt;/li&gt;
    &lt;li&gt;&lt;p&gt;&quot;&lt;i&gt;Remote fixed-cost&lt;/i&gt;” services: costly enough to host that they’re not offered for free,
          but where the load you personally contribute is still sufficiently
          bounded that a flat-rate subscription pricing model suffices.
    &lt;/li&gt;
    &lt;li&gt;&lt;p&gt;Plus a comparably small amount of “&lt;i&gt;remote compute services&lt;/i&gt;”: e.g. photo storage; paid for intentionally by the user, with
          variable costs or tiered plans.
    &lt;/li&gt;
  &lt;/ol&gt;
&lt;p&gt;The paradigm of yore was “&lt;i&gt;local general-purpose&lt;/i&gt;” - you had a computer or device, and used it however you saw fit; the
      networks didn’t really exist to support anything else. The internet then
      ushered in an era of “&lt;i&gt;ad-supported&lt;/i&gt;” services, e.g. search engines, maps, and social networks. More recently
      yet “&lt;i&gt;Remote fixed-cost&lt;/i&gt;” has been on the rise, with services like Spotify or Netflix leading the
      charge into the home from the media front with VPNs riding on their
      coattails, team-oriented services like Zoom gaining significant mindshare
      in the “free for home, paid for business” space, and most recently “&lt;a
      href=&quot;https://en.wikipedia.org/wiki/Cloud_gaming&quot;&gt;cloud gaming&lt;/a&gt;” starting to take off as well. And finally “&lt;i&gt;remote compute services&lt;/i&gt;” exist in limited form, particularly for storage applications, but not
      yet having taken off.
  &lt;/p&gt;
  &lt;p&gt;(Tangent: I looked up the Adobe Creative Cloud and other competitors to
      see if “paid cloud photo/video editing” is a thing, and it seems not yet?
      I’m a little surprised, as data-heavy applications that idle at 0 but
      could easily spike to hundreds of cores during active use seems inherently
      well suited to this model.)
  &lt;/p&gt;
  &lt;p&gt;You’ll note that I did not include “&lt;i&gt;remote general-purpose&lt;/i&gt;” in the list above, as I do not see this as a prevalent paradigm &lt;i&gt;today&lt;/i&gt;, with a bit of niche usage of “&lt;a
      href=&quot;https://en.wikipedia.org/wiki/Desktop_virtualization&quot;&gt;virtual desktop in the Cloud&lt;/a&gt;” style services and little else. But it’s likely coming next - I expect
      the consumer portion of computing to shift in decent part towards remote
      variable-cost compute as the local slice of compute consumption becomes
      relatively smaller, through some mix of proliferating “&lt;i&gt;remote compute services&lt;/i&gt;” offering pay-as-you-go or subscription access to myriad services for
      storage, photo/video editing, data analysis, model generation, data
      generation, etc; as well as new “&lt;i&gt;remote general-purpose&lt;/i&gt;” paradigms that have yet to be developed, such as “apps” executing on
      dynamic remote resources.&lt;/p&gt;
  &lt;p&gt;While this &lt;i&gt;could&lt;/i&gt;
      be presented as “desktop in the Cloud”, I doubt it will be - the desktop
      paradigm is suited to a small, always on, fixed-sized, pre-purchased unit
      of compute (one computer), and less clearly suited to expressing irregular
      bursts of short-lived high-consumption activities. Solving for burst usage
      with cost predictability will likely require developing paradigms more
      inherently suited to that task, rather than simply transplanting a
      (relatively) dying paradigm into “the Cloud”. It’s much more likely to me
      that subscription services will take the broadest share, and decently
      likely that a cost-predictable paradigm for “remote apps” will be found to
      fill the remaining gap.
&lt;/p&gt;
&lt;p&gt;As a final thought to leave you with, the donation of spare cycles towards
    causes of interest may be interesting in a future world also. Today imagine
  collectivist projects like &lt;a href=&quot;https://foldingathome.org/&quot;&gt;Folding@Home&lt;/a&gt;, or seeding torrents to challenge content “ownership”. Will these still
  exist, and in what form?&lt;/p&gt;
</content><link rel='replies' type='application/atom+xml' href='http://www.thelowlyprogrammer.com/feeds/7774054165909597425/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.thelowlyprogrammer.com/2021/04/the-rise-of-datacenter-computing.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1806360094658697411/posts/default/7774054165909597425'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1806360094658697411/posts/default/7774054165909597425'/><link rel='alternate' type='text/html' href='http://www.thelowlyprogrammer.com/2021/04/the-rise-of-datacenter-computing.html' title='The Rise of Datacenter Computing'/><author><name>Eric Burnett</name><uri>http://www.blogger.com/profile/10741882872804697111</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1806360094658697411.post-2314222549190704708</id><published>2012-11-01T15:53:00.000-04:00</published><updated>2012-11-12T16:31:09.553-05:00</updated><title type='text'>Hardware trends for datacenter computing</title><content type='html'>&lt;p&gt;Moore’s Law (and family) are a significant boon to anyone writing distributed systems. As the systems grow over time, so too does the underlying hardware get more powerful, allowing exponential growth to be readily managed. Programs need to be modified to cope, of course, but the raw compute power is there and available. That said, designing algorithms that can scale with more than an order of magnitude of growth remains a difficult challenge, and regular rewrites are inevitable. In the interest of minimizing that number, here are a few trends I’ve observed that should continue into the next few years. Caveat emptor, this is my personal perspective, but if it doesn&#39;t line up with your experience I&#39;d love to hear about it.&lt;/p&gt;&lt;h2&gt;1. Compute Units are Static&lt;/h2&gt;&lt;p&gt;&lt;ol type=&quot;a&quot;&gt;&lt;li&gt;Moore’s Law tells us that the number of transistors on a die double every 2 years, and practical experience tells us that performance doubles every 18 months. In the multicore era, however, the performance of a single core has largely plateaued, with further advances coming in the form of more cores per chip.&lt;/li&gt;
&lt;li&gt;RAM and on-die caches roughly keep pace with CPUs, which means that now that we’re into core count growth, the RAM and cache per core is now approximately fixed.&lt;/li&gt;
&lt;li&gt;Disk storage is also keeping the same pace, and hence disk per core is now also approximately fixed.&lt;/li&gt;
&lt;/ol&gt;&lt;/p&gt;&lt;p&gt;Thus, we can define a theoretical “compute unit” as, say, 1 core, 2GB RAM, 20GB flash*, 200 GB disk (adjust numbers as you see fit); this unit should stay roughly stable going forward, with the number of such units increasing exponentially.&lt;/p&gt;&lt;p&gt;Note that the purchasable range of resources-per-core is generally increasing; this represents the most economical average rather than a strict upper or lower bound. Resources do not need to be apportioned to each process in these ratios, either; e.g. disk-heavy and CPU-heavy processes can coexist on a machine and balance each other out. But it&#39;s worth looking closely at anything expected to grow significantly on one axis and not in others, to see if it&#39;s sustainable long term.&lt;/p&gt;&lt;p&gt;* Flash adoption remains spotty, but early indications put it roughly halfway between RAM and disk on performance and price, and it seems to be keeping up the growth pace. I speculate it will become a standard inclusion going forward, but this isn’t as clear as the others.&lt;/p&gt;&lt;p&gt;&lt;b&gt;Update:&lt;/b&gt; One of my colleagues has pointed out that disk performance is not keeping pace with capacity increases (neither seek nor transfer rates). I&#39;m not sure how to include this detail in the compute unit definition itself, but it will likely prove to be significant and help drive the adoption of flash going forward.&lt;/p&gt;&lt;h2&gt;2. The Datacenter is the New Machine&lt;/h2&gt;&lt;p&gt;This is already largely true, but the trend will only get stronger. This is driven by two main trends:&lt;br /&gt;
&lt;ol type=&quot;a&quot;&gt;&lt;li&gt;NUMA architectures are reducing sharing and performance uniformity within machines, and&lt;/li&gt;
&lt;li&gt;Intra-datacenter connectivity is growing faster than individual machine performance, reducing the impact of communicating between machines and racks.&lt;/li&gt;
&lt;/ol&gt;&lt;/p&gt;&lt;p&gt;Thus, the trend of running programs as many smaller components that may or may not share any particular physical hardware will continue to get stronger. Correspondingly, virtualization will be increasingly necessary for isolation, further abstracting processes from the hardware they run on.&lt;/p&gt;&lt;p&gt;We may also start to see this trend spread to the client-side as well, but I’m not sure what form it would take. In-home compute sharing?&lt;/p&gt;&lt;h2&gt;3. Decomposable Programs are Ideal&lt;/h2&gt;&lt;p&gt;As a corollary to the previous point, algorithms that can be decomposed into fixed-sized chunks are best. They can be apportioned among CPUs/machines/racks easily, and scaling is decoupled from machine growth (which occurs in generational bursts when hardware is refreshed). Which means that if sharding is necessary, dynamic sharding - a variable number of fixed-size chunks - should be preferred over having a fixed number of shards that grow separately.&lt;/p&gt;&lt;p&gt;MapReduce (or Hadoop), Pregel (or Giraph), and GFS (or HDFS) are all examples of decomposable programs, tackling data processing, graph computation, and storage, respectively.&lt;/p&gt;&lt;h2&gt;4. Consumer Bandwidth is Lagging&lt;/h2&gt;&lt;p&gt;Consumer network growth is lagging behind intra- and inter-datacenter networks, and hardware trends. Some of this is due to a shift towards mobile, where equivalent resources (primarily images and videos) are smaller, but it holds on wired internet as well. It is unclear to me how long this will last - gigabit customer internet is already being tested in a few places, for example - but for the time being it remains true. This means inter-datacenter communication is growing cheaper relative to communicating with consumers. While it remains true we should expect more work to be pushed remotely to the “cloud” and an increase in datacenter entities cross communicating, and a slow shift towards smaller and more expensive resources (heavier compression, more computationally expensive codecs, etc) for customers.&lt;/p&gt;&lt;h2&gt;5. Latency Doesn’t Scale&lt;/h2&gt;&lt;p&gt;A fact that has always remained true: the speed of light is (effectively) constant, and does not decrease in line with other improvements. As bandwidth increases latency will continue to dominate long-distance performance, and round-trip times will continue to constrain serial throughput. Together, these will continue to push a couple of long-lived trends:&lt;/p&gt;&lt;p&gt;&lt;ol type=&quot;a&quot;&gt;&lt;li&gt;Reducing data proximity. Caching (local and edge) and having data prefetched are key in reducing latency, and get comparatively cheaper over time. One could imagine future efforts that will proactively push resources straight into user networks or devices as well.&lt;/li&gt;
&lt;li&gt;Supporting long-lived connections and message parallelism. Connection establishment requires at least one round trip, and any form of serial channel use cannot fully utilize one. Together these will lead to more connection sharing where possible, and more long-lived idle connections otherwise.&lt;/li&gt;
&lt;/ol&gt;&lt;/p&gt;&lt;h2&gt;Further Reading:&lt;/h2&gt;&lt;p&gt;&lt;a href=&quot;http://www.ieee802.org/3/ad_hoc/bwa/BWA_Report.pdf&quot;&gt;IEEE Industry Connections Ethernet Bandwidth Assessment&lt;/a&gt;&lt;br /&gt;
(Thorough study of bandwidth trends at all levels)&lt;/p&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.thelowlyprogrammer.com/feeds/2314222549190704708/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.thelowlyprogrammer.com/2012/11/hardware-trends-for-datacenter-computing.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1806360094658697411/posts/default/2314222549190704708'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1806360094658697411/posts/default/2314222549190704708'/><link rel='alternate' type='text/html' href='http://www.thelowlyprogrammer.com/2012/11/hardware-trends-for-datacenter-computing.html' title='Hardware trends for datacenter computing'/><author><name>Eric Burnett</name><uri>http://www.blogger.com/profile/10741882872804697111</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1806360094658697411.post-4728624078059707982</id><published>2012-08-26T10:41:00.001-04:00</published><updated>2021-07-17T22:51:56.815-04:00</updated><title type='text'>Primes part 2: A segmented sieve</title><content type='html'>&lt;script language=&#39;JavaScript&#39; type=&#39;text/javascript&#39;&gt;
function expandContract(id) {
  var e = document.getElementById(id);
  if (e.style.display == &quot;block&quot;) {
    e.style.display = &quot;none&quot;;
  } else {
    e.style.display = &quot;block&quot;;
  }
}
&lt;/script&gt;&lt;p&gt;&lt;strong&gt;TL;DR: The sum of all primes &lt;= 1,000,000,000,000 is 18,435,588,552,550,705,911,377&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;&lt;i&gt;This post is a followup to &lt;a href=&quot;http://www.thelowlyprogrammer.com/2010/03/writing-efficient-seive-of-eratosthenes.html&quot;&gt;Writing an efficient Sieve of Eratosthenes&lt;/a&gt;&lt;/i&gt;&lt;/p&gt;&lt;p&gt;A while back I wrote a post detailing a memory-efficient &lt;a href=&quot;https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes&quot;&gt;Sieve of Eratosthenes&lt;/a&gt;. The algorithm I used took advantage of lazy evaluation and a sliding array to reduce the RAM requirement to a fraction of what a &#39;vanilla&#39; implementation would require, at the cost of a non-trivial increase in CPU time. The resulting code ran at approximately 25% the throughput of the vanilla implementation, and maxed out at 2 billion candidates.&lt;/p&gt;&lt;p&gt;While researching that post, I noted that the most efficient generators at present use either the &lt;a href=&quot;https://en.wikipedia.org/wiki/Sieve_of_Atkin&quot;&gt;Sieve of Atkin&lt;/a&gt; or a &#39;segmented sieve&#39;. As an excuse to play with &lt;a href=&quot;https://golang.org/&quot;&gt;Go&lt;/a&gt;&lt;sup&gt;[&lt;a href=&quot;#ss_footnote1&quot;&gt;1&lt;/a&gt;]&lt;/sup&gt;, a couple weeks ago I decided to implement a segmented Sieve of Eratosthenes. This post details my results.&lt;/p&gt;&lt;h2&gt;Implementation&lt;/h2&gt;&lt;u&gt;Language:&lt;/u&gt; Go&lt;u&gt;&lt;br /&gt;
Time complexity:&lt;/u&gt;&lt;b&gt; &lt;/b&gt;O(n&amp;#8729;log(log(n)))&lt;br /&gt;
&lt;u&gt;Space complexity&lt;/u&gt;&lt;b&gt;: &lt;/b&gt;O(&amp;#8730;n)&lt;br /&gt;
&lt;u&gt;Candidates in 1 sec:&lt;/u&gt; ~50,000,000 &lt;br /&gt;
&lt;br /&gt;
&lt;a href=&quot;https://gist.github.com/320271#file_segmented_primes.go&quot;&gt;Gist&lt;/a&gt; &lt;a href=&quot;javascript:expandContract(&#39;segmented_primes.go&#39;)&quot;&gt;(expand/contract)&lt;/a&gt;&lt;br /&gt;
&lt;div id=&quot;segmented_primes.go&quot; class=&quot;expandable&quot; style=&quot;display:none;&quot;&gt;&lt;script src=&quot;https://gist.github.com/320271.js?file=segmented_primes.go&quot;&gt;&lt;/script&gt;&lt;/div&gt;&lt;br /&gt;
&lt;p&gt;The algorithm proceeds as follows:&lt;br /&gt;
&lt;ol&gt;&lt;li&gt;Calculate primes up to &amp;#8730;max via a vanilla array sieve&lt;/li&gt;
&lt;li&gt;Slice up segments of about &amp;#8730;max candidates for checking&lt;/li&gt;
&lt;li&gt;To check a range,&lt;br /&gt;
&lt;ol&gt;&lt;li&gt;For each prime p from 1., find the first multiple within the range that&#39;s &gt;= p&lt;sup&gt;2&lt;/sup&gt;&lt;/li&gt;
&lt;li&gt;Cross off every multiple from there to the end of the range&lt;/li&gt;
&lt;/ol&gt;&lt;/li&gt;
&lt;li&gt;Merge the results from the processed segments&lt;/li&gt;&lt;/ol&gt;&lt;/p&gt;&lt;p&gt;You&#39;ll note that other than splitting the full candiate set into segments, this is the standard Sieve of Eratosthenes. Hence, it&#39;s the &lt;i&gt;segmented&lt;/i&gt; Sieve of Eratosthenes.&lt;/p&gt;&lt;p&gt;In my Go version this is implemented by starting segments as individual goroutines that output to their own channels. A single worker goroutine is responsible for marshaling the results from these channels to a single channel read by the main thread. This architecture was chosen simply because it fits well with the Go way of doing things, but it also has the side-effect of providing some amount of free parallelism.&lt;/p&gt;&lt;h2&gt;Results&lt;/h2&gt;&lt;p&gt;The very first run of this variant was faster than the most optimized version from my previous post. It runs at about 65% the speed of a vanilla implementation, making it about 2.5x as efficient as the previous lazy implementations, with a lower memory footprint. As always, a better algorithm is worth more than any amount of low level code tuning :). I should point out that in the current implementation I also implemented a bit array rather than simply using an array of bools. This reduced the memory footprint somewhat, but did not appear to have any significant impact in either direction on CPU time required, and so could reasonably be dropped to shorten the code.&lt;/p&gt;&lt;p&gt;With all primes needing to be marshaled back to the main thread parallelism maxes out below 2x linear. If all we care about is an aggregate value computed from the primes (the sum in this case), rather than the primes themselves in order, we can achieve &gt;4x parallelism simply by adding more processes. This is also more efficient in general, and allows &gt;300,000,000 primes to be processed in one second&lt;sup&gt;[&lt;a href=&quot;#ss_footnote2&quot;&gt;2&lt;/a&gt;]&lt;/sup&gt;.&lt;/p&gt;&lt;p&gt;The net result is an implementation that can sieve 50M candidates in one second on one core or sum 300M on four; sum primes up to one trillion in an hour; or sum primes in a range of one billion (10^9) in the region of one quintillion (10^18) in under a minute. I&#39;m happy with that...for now.&lt;/p&gt;&lt;h2&gt;Footnotes&lt;/h2&gt;&lt;ol&gt;&lt;li&gt;&lt;a name=&quot;ss_footnote1&quot;&gt;Let me say right now that Go is a fantastic language to work with, being both easy to write and efficient to run. I fully intend to start writing some of my work projects in Go in the near future.&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a name=&quot;ss_footnote2&quot;&gt;As noted in the previous post, we use the generic &quot;primes in one second&quot; metric for making apples-to-oranges comparisons of algorithms and implementations. This is not intended to provide anything more than a rough comparison.&lt;/a&gt;&lt;/li&gt;&lt;/ol&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.thelowlyprogrammer.com/feeds/4728624078059707982/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.thelowlyprogrammer.com/2012/08/primes-part-2-segmented-sieve.html#comment-form' title='3 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1806360094658697411/posts/default/4728624078059707982'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1806360094658697411/posts/default/4728624078059707982'/><link rel='alternate' type='text/html' href='http://www.thelowlyprogrammer.com/2012/08/primes-part-2-segmented-sieve.html' title='Primes part 2: A segmented sieve'/><author><name>Eric Burnett</name><uri>http://www.blogger.com/profile/10741882872804697111</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>3</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1806360094658697411.post-9039816403040700829</id><published>2012-08-19T09:24:00.001-04:00</published><updated>2021-07-17T22:51:36.152-04:00</updated><title type='text'>The algorithms of memory</title><content type='html'>&lt;p&gt;The human brain has the best storage system on the block in a lot of ways. It’s notably lossy and doesn’t have the easiest API to work with, but in terms of flexibility it’s second to none. Computer scientists have been trying to model and mimic its features for a lot of years, but we haven’t got it cracked quite yet. Part of the challenge lies in the sheer variety of access styles that human memory allows. I don’t think we even have them all &lt;i&gt;cataloged&lt;/i&gt; yet, much less working together in one system.&lt;/p&gt;&lt;p&gt;I’ve been trying over the last couple days to list all the different patterns I can see myself using. I’ve also tried to pull out systems I know of that do the same for comparison, although I can’t find direct equivalents in all cases. Those &lt;i&gt;without&lt;/i&gt; an existing equivalent are probably the most interesting - would mimicking these patterns be useful in the future?&lt;/p&gt;&lt;br /&gt;
&lt;style type=&quot;text/css&quot;&gt;
.memtable {border-collapse: collapse; padding: 2px;}
.memtable tr {border: 1px solid #C3C3C3; padding: 2px; spacing: 0px; vertical-align:center; text-align:center;}
.memtable th {padding: 4px; spacing: 0px; vertical-align:center; text-align:center;}
.memtable td {padding: 4px; spacing: 0px;}
&lt;/style&gt;&lt;table class=&quot;memtable&quot;&gt;&lt;colgroup&gt;&lt;col&gt;&lt;/col&gt;&lt;col width=&quot;33%&quot;&gt;&lt;/col&gt;&lt;col width=&quot;33%&quot;&gt;&lt;/col&gt;&lt;col width=&quot;33%&quot;&gt;&lt;/col&gt;&lt;/colgroup&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;th style=&quot;min-width: 90px;&quot;&gt;&lt;strong&gt;Name&lt;/strong&gt;&lt;/th&gt;&lt;th&gt;&lt;strong&gt;Mind example&lt;/strong&gt;&lt;/th&gt;&lt;th&gt;&lt;strong&gt;System example&lt;/strong&gt;&lt;/th&gt;&lt;th&gt;&lt;strong&gt;Characterized by&lt;/strong&gt;&lt;/th&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;Cache hit&lt;/td&gt;&lt;td&gt;Facts immediately available for use with no delay&lt;/td&gt;&lt;td&gt;In-memory data storage&lt;/td&gt;&lt;td&gt;Synchronous; low latency&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;Long term storage&lt;/td&gt;&lt;td&gt;Facts that take a while to look up. “Um...his name... was.... Let&#39;s move on - it&#39;ll come to me in a minute.”&lt;/td&gt;&lt;td&gt;Lookups from disk or tape&lt;/td&gt;&lt;td&gt;Asynchronous (notify on delivery); high latency&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;Reminders&lt;/td&gt;&lt;td&gt;Remembering to go buy groceries on your way home from work&lt;/td&gt;&lt;td&gt;Calendar notifications&lt;/td&gt;&lt;td&gt;Time or event based; defined in advance&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;Information requests&lt;/td&gt;&lt;td&gt;All the associations that come up when you think of a topic. “New Orleans” brings to mind...&lt;/td&gt;&lt;td&gt;Web search&lt;/td&gt;&lt;td&gt;Web of relationships; can be explored further in any direction&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;Background processing&lt;/td&gt;&lt;td&gt;Coming up with answers/solutions while you sleep or otherwise aren’t explicitly working on the problem&lt;/td&gt;&lt;td&gt;Uploading a video to YouTube - when you check again all the different formats and qualities will be available&lt;/td&gt;&lt;td&gt;Processing item while in storage; queued work requests; separated from foreground tasks&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;Full Scan&lt;/td&gt;&lt;td&gt;Life flashing before your eyes (&lt;a href=&quot;https://en.wikipedia.org/wiki/Life_review&quot;&gt;wiki&lt;/a&gt;)&lt;/td&gt;&lt;td&gt;Processing ordered events during crash recovery&lt;/td&gt;&lt;td&gt;Ordered; sequential scan of all items&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;Guided randomness&lt;/td&gt;&lt;td&gt;Brainstorming, free association games, mad libs, improv&lt;/td&gt;&lt;td&gt;?&lt;/td&gt;&lt;td&gt;Random item output or random exploration of web; subject to limited constraints&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;Unsolicited triggered reminders&lt;/td&gt;&lt;td&gt;Being reminded of facts/stories/events by things you see or hear&lt;/td&gt;&lt;td&gt;? [&lt;a href=&quot;#mem_footnote1&quot;&gt;1&lt;/a&gt;]&lt;br /&gt;
&lt;/td&gt;&lt;td&gt;Unsolicited notifications; loosely related to recent input&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;Unsolicited untriggered reminders&lt;/td&gt;&lt;td&gt;Memories that come to mind with no discernible trigger, e.g. past regrets&lt;/td&gt;&lt;td&gt;?&lt;/td&gt;&lt;td&gt;Unsolicited notifications; no relation to recent input; may be randomly derived&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;State affecting&lt;/td&gt;&lt;td&gt;Memories that change your mood or attitude. E.g. remembering a birthday makes you happy; remembering specific near miss makes you cautious.&lt;/td&gt;&lt;td&gt;? [&lt;a href=&quot;#mem_footnote2&quot;&gt;2&lt;/a&gt;]&lt;/td&gt;&lt;td&gt;State changes triggered by the contents of the information retrieved&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;Expectations&lt;br /&gt;
&lt;font color=&quot;#aaaaaa&quot; style=&quot;font-size:smaller;&quot;&gt;(suggested by &lt;a href=&quot;https://www.reddit.com/user/e-socrates&quot;&gt;e-socrates&lt;/a&gt; on Reddit)&lt;/font&gt;&lt;/td&gt;&lt;td&gt;&quot;We constantly predict from memory what to expect, then compare that to experience&quot;&lt;/td&gt;&lt;td&gt;?&lt;/td&gt;&lt;td&gt;Continuous short-term predictions; characterizing events as unexpected or surprising&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;&lt;h2&gt;Footnotes&lt;/h2&gt;&lt;p&gt;&lt;ul&gt;&lt;li&gt;[&lt;a name=&quot;mem_footnote1&quot;&gt;1&lt;/a&gt;] Google Now is trying for this to some extent. Online advertising partially fits, however it is not bubbling up details you already know - rather, it’s feeding you new data.&lt;/li&gt;
&lt;li&gt;[&lt;a name=&quot;mem_footnote2&quot;&gt;2&lt;/a&gt;] There are some trigger-based examples of this in security, e.g. logging of accesses to documents, but they don’t really change the state of the system itself (they trigger work in others instead).&lt;/li&gt;
&lt;/ul&gt;&lt;/p&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.thelowlyprogrammer.com/feeds/9039816403040700829/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.thelowlyprogrammer.com/2012/08/the-algorithms-of-memory.html#comment-form' title='3 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1806360094658697411/posts/default/9039816403040700829'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1806360094658697411/posts/default/9039816403040700829'/><link rel='alternate' type='text/html' href='http://www.thelowlyprogrammer.com/2012/08/the-algorithms-of-memory.html' title='The algorithms of memory'/><author><name>Eric Burnett</name><uri>http://www.blogger.com/profile/10741882872804697111</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>3</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1806360094658697411.post-5436567093424877225</id><published>2012-01-30T00:05:00.000-05:00</published><updated>2012-01-30T00:05:33.736-05:00</updated><title type='text'>On hosting static files</title><content type='html'>&lt;p&gt;You may have noticed that I have a blog. Shocking, I know. It’s predominantly composed of text, as many are, but that’s not the &lt;em&gt;only&lt;/em&gt; content I like to share. Posts are more approachable and memorable if they contain images, &lt;a title=&quot;Marriage Sort, a visualization&quot; href=&quot;http://www.thelowlyprogrammer.com/2012/01/marriage-sort-visualization.html&quot;&gt;sorting visualizations&lt;/a&gt; require Javascript frameworks, etcetera. Unfortunately, all these support files need to be stored somewhere.&lt;/p&gt;&lt;p&gt;This blog is hosted on Blogger, a decision I’m very happy with. Blogger doesn’t do arbitrary file hosting (that I know of), but it &lt;em&gt;does&lt;/em&gt; support pushing images to &lt;a title=&quot;Picasa Web Albums&quot; href=&quot;http://picasaweb.google.com&quot;&gt;Picasa&lt;/a&gt;. So images are hosted by Picasa, another Google service, no sweat. Now, what about other files?&lt;/p&gt;&lt;p&gt;Hmm.&lt;/p&gt;&lt;p&gt;It turns out the answer to this question is a lot trickier than I would have hoped. I was hoping for another Google service, to keep my dependencies down, so I started with Sites.&lt;/p&gt;&lt;h2&gt;&lt;/h2&gt;&lt;h2&gt;&lt;/h2&gt;&lt;h2&gt;Google Sites &lt;/h2&gt;&lt;p&gt;“&lt;em&gt;Google Sites&lt;/em&gt; is a free and easy way to create and share webpages”, the tagline goes. And it is – we use it internally all the time. One quick form later, I have a site, and another couple clicks puts a single attachment on it. Great! Update the post to reference it, double-check in incognito mode, and my work here is done. Right?&lt;/p&gt;&lt;p&gt;Well…not quite. It turns out that Sites uses some interesting redirect magic to actually link you to your content. Redirect magic that, for reasons I don’t understand, doesn’t actually resolve when you’re using it to hotlink content. For exactly this reason I guess, I don’t know. Anyways, since I had visited this content &lt;em&gt;while on Sites&lt;/em&gt;, my browser had it cached, and even incognito mode could access it, but it wouldn’t resolve anywhere else. Which is a good reason to test things on more than one computer, I suppose.&lt;/p&gt;&lt;p&gt;Ok, not sites. App Engine?&lt;/p&gt;&lt;h2&gt;Google App Engine&lt;/h2&gt;&lt;p&gt;App Engine &lt;em&gt;does&lt;/em&gt; do static file hosting. Instructions are &lt;a title=&quot;Google App Engine - Using Static Files&quot; href=&quot;http://code.google.com/appengine/docs/python/gettingstarted/staticfiles.html&quot;&gt;here&lt;/a&gt; – you must simply download the SDK, create the app.yaml file appropriately, upload your new App (the files must belong to something, after all), etc. This looks doable, but certainly non-trivial. I also cannot figure out how this is going to be priced, nor the latency to expect from it. So let’s keep looking.&lt;/p&gt;&lt;p&gt;I’m running out of Google services (I considered and rejected a few more). Time to look further afield. I don’t know of any good, trustworthy solutions offhand, and a quick search isn’t taking me anywhere I really want to go, so lets look at Amazon AWS. They have a service for every occasion, right?&lt;/p&gt;&lt;h2&gt;Amazon AWS&lt;/h2&gt;&lt;p&gt;As it turns out, yes, yes they do. A quick look through the &lt;a title=&quot;Amazon Web Services - Products &amp;amp; Services&quot; href=&quot;http://aws.amazon.com/products/&quot;&gt;options&lt;/a&gt; (there are so many!) says that Amazon &lt;a title=&quot;Amazon Simple Storage Service&quot; href=&quot;http://aws.amazon.com/s3/&quot;&gt;Simple Storage Service&lt;/a&gt; (S3) will do nicely, optionally backed by &lt;a title=&quot;Amazon CloudFront&quot; href=&quot;http://aws.amazon.com/cloudfront/&quot;&gt;CloudFront&lt;/a&gt; if I ever really need to scale. A quick signup, upload of my files, and one new &lt;a title=&quot;Customizing Amazon S3 URLs with CNAME&quot; href=&quot;http://imtips.co/customizing-amazon-s3-urls-with-cname.html&quot;&gt;CNAME&lt;/a&gt;, and I’ve got my files living under &lt;em&gt;static.thelowlyprogrammer.com&lt;/em&gt;, backed by S3. Nice! Doubly so since the free usage tier should make this entirely free for the next year or so, and pennies after.&lt;/p&gt;&lt;p&gt;Finally, in researching this blog post, I found one more option. And it’s a Google service, which was my original goal. Let’s check it out!&lt;/p&gt;&lt;h2&gt;Google Cloud Storage&lt;/h2&gt;&lt;p&gt;New on the block, Google Cloud Storage appears to be a competitor to Amazon S3. Priced a tiny bit cheaper, the features seem roughly comparable. The documentation is a bit rough still, which partly explains why I didn’t figure this out earlier, but everything is there if you look hard enough. One significant distinction is that you do not choose where data is stored (unless you want to add restrictions), and it will get replicated wherever needed. Note that this &lt;em&gt;includes&lt;/em&gt; automatic edge caching for popular files, so this is pretty much S3 and CloudFront all rolled into one. Fancy! It supports the same &lt;a title=&quot;Google Cloud Storage - Request URIs&quot; href=&quot;https://developers.google.com/storage/docs/reference-uris&quot;&gt;CNAME aliases&lt;/a&gt;, so I’ve got this set up as a hot-spare for my S3 data. I’ll leave S3 as primary for now since I’ve got it all configured and tested happily, but it looks like I’d be well served either way.&lt;/p&gt;&lt;p&gt;Maybe in a future post I’ll do a head-to-head comparison of S3 and GCS, if I can figure out a fair way of measuring performance all over the globe. Until then, I’m happy to stick with either.&lt;/p&gt;&lt;p&gt;Mission accomplished – static files hosted. Time for bed.&lt;/p&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.thelowlyprogrammer.com/feeds/5436567093424877225/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.thelowlyprogrammer.com/2012/01/on-hosting-static-files.html#comment-form' title='3 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1806360094658697411/posts/default/5436567093424877225'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1806360094658697411/posts/default/5436567093424877225'/><link rel='alternate' type='text/html' href='http://www.thelowlyprogrammer.com/2012/01/on-hosting-static-files.html' title='On hosting static files'/><author><name>Eric Burnett</name><uri>http://www.blogger.com/profile/10741882872804697111</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>3</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1806360094658697411.post-415822424499778135</id><published>2012-01-16T23:49:00.002-05:00</published><updated>2021-07-17T23:11:22.152-04:00</updated><title type='text'>Marriage Sort, a visualization</title><content type='html'>&lt;i&gt;You&#39;ll need a modern browser that handles SVG (a recent Chrome, Firefox, or IE9 will do) to properly see this post.&lt;/i&gt;&lt;br /&gt;
&lt;br /&gt;
On Saturday, &lt;a href=&quot;http://bost.ocks.org/mike/&quot;&gt;Mike Bostock&lt;/a&gt; posted a link on Hacker News to &lt;a href=&quot;http://bost.ocks.org/mike/shuffle/&quot;&gt;an explanation of the Fisher-Yates Shuffle&lt;/a&gt; he had written. Alongside the explanation he included a very slick visualization where you could see it in action. Now, at a fundamental level, shuffle algorithms and sorting algorithms are simply the reverse of each other. So if this visualization does well on shuffle algorithms, why not a sorting algorithm as well?&lt;br /&gt;
&lt;br /&gt;
A couple years ago I wrote a sorting algorithm, which I gave the tongue-in-cheek name Marriage Sort (an homage to the problem that inspired it - see &lt;a href=&quot;http://www.thelowlyprogrammer.com/2010/04/introducing-marriage-sort.html&quot;&gt;the introduction post&lt;/a&gt; for more details). I was looking for a project to play with on the weekend, so I thought, why not try the visualization on that? Mike has kindly let me re-use his code, so here&#39;s the same visualization of Marriage Sort.&lt;br /&gt;
&lt;div id=&quot;chart&quot;&gt;&lt;/div&gt;&lt;style&gt;

.line {
  stroke: #999;
  stroke-width: 2px;
}

.play path {
  stroke: #fff;
  stroke-width: 6px;
}

.play:hover path {
  fill: red;
}

.play rect {
  fill: none;
  pointer-events: all;
  cursor: pointer;
}

#disclaimer {
  text-align: center;
  width: 548px;
  margin-left: 24px;
  margin-right: 24px;
}

&lt;/style&gt;&lt;br /&gt;
&lt;script src=&quot;http://static.thelowlyprogrammer.com/files/d3/2.7.3/d3.min.js&quot;&gt;&lt;/script&gt;&lt;script&gt;

var v_margin = {top: 10, right: 24, bottom: 10, left: 24},
    v_width = 500,
    v_height = 60 - v_margin.top - v_margin.bottom,
    v_size = v_height * .8;

var v_n = 60;

var v_tick = 600,
    v_animate = 450;  // Should be a multiple of 3.

var COLOR_IDLE = &quot;#999&quot;,
    COLOR_DONE = &quot;#000&quot;,
    COLOR_ANIMATING = &quot;#000&quot;,
    COLOR_KEY = &quot;red&quot;,
    COLOR_PARTITION = &quot;#900&quot;,
    COLOR_KEY_ANIMATING = &quot;red&quot;;

var STROKE = &quot;2px&quot;,
    STROKE_BOLD = &quot;4px&quot;;

var v_x = d3.scale.ordinal()
    .domain(d3.range(v_n))
    .rangePoints([0, v_width]);

var v_y = d3.scale.linear()
    .domain([0, v_n - 1])
    .range([-Math.PI / 6, Math.PI / 6]);

// In place shuffle.
function shuffle(list) {
  for (rem = list.length; rem &gt; 0; --rem) {
    var next = Math.floor(Math.random() * rem)  // [0..rem)
    var tmp = list[next]
    list[next] = list[rem-1];
    list[rem-1] = tmp;
  }
  return list;
}

// Swap of two lines. Colors can optionally be specified for during the transition,
// however post-transition all lines are made black.
function swap(line, a, b, color_a, color_b) {
  if (color_a == undefined) {
    color_a = COLOR_ANIMATING;
  }
  if (color_b == undefined) {
    color_b = COLOR_ANIMATING;
  }
  if (a == b ) {
    d3.select(line[0][a])
        .style(&quot;stroke&quot;, COLOR_DONE)
        .style(&quot;stroke-width&quot;, STROKE);
    return;
  } else if (a &gt; b) {
    // Swap indices for simplicity.
    var t = b;
    b = a;
    a = t;
    t = color_b;
    color_b = color_a;
    color_a = t;
  }
  var b_out = line[0].splice(b, 1)[0],
      a_out = line[0].splice(a, 1)[0];
  line[0].splice(a, 0, b_out);
  line[0].splice(b, 0, a_out);

  d3.select(a_out)
      .style(&quot;stroke&quot;, color_a)
      .style(&quot;stroke-width&quot;, STROKE_BOLD)
    .transition()
      .duration(v_animate)
      .attr(&quot;transform&quot;, &quot;translate(&quot; + v_x(b) + &quot;, &quot; + v_height + &quot;)&quot;);
  setTimeout(function() {
    d3.select(a_out)
        .style(&quot;stroke&quot;, COLOR_DONE)
        .style(&quot;stroke-width&quot;, STROKE);
  }, v_animate);

  d3.select(b_out)
      .style(&quot;stroke&quot;, color_b)
      .style(&quot;stroke-width&quot;, STROKE_BOLD)
    .transition()
      .duration(v_animate)
      .attr(&quot;transform&quot;, &quot;translate(&quot; + v_x(a) + &quot;, &quot; + v_height + &quot;)&quot;);
  setTimeout(function() {
    d3.select(b_out)
        .style(&quot;stroke&quot;, COLOR_DONE)
        .style(&quot;stroke-width&quot;, STROKE);
  }, v_animate);
}

// Remove line at &#39;src&#39; and place it at &#39;dst&#39;, moving over all the intermediate
//  lines in the process. src will end up immediately to the left of the value
//  currently in &#39;dst&#39;.
function insert(line, src, dst) {
  if (dst - src == 2) {
    swap(line, src, src+1);
    return;
  } else if (src - dst == 1) {
    swap(line, src, dst);
    return;
  }

  var out = line[0].splice(src, 1)[0];
  if (src &gt; dst) {
    line[0].splice(dst, 0, out);
  } else {
    line[0].splice(dst-1, 0, out);
  }

  // Animate primary line:
  d3.select(out)
      .style(&quot;stroke&quot;, COLOR_ANIMATING)
      .style(&quot;stroke-width&quot;, STROKE_BOLD)
    .transition()
      .duration(v_animate)
      .attr(&quot;transform&quot;, &quot;translate(&quot; + v_x(src &gt; dst ? dst : dst-1) + &quot;, &quot; + v_height + &quot;)&quot;);
  setTimeout(function() {
    d3.select(out)
        .style(&quot;stroke&quot;, COLOR_DONE)
        .style(&quot;stroke-width&quot;, STROKE);
  }, v_animate);

  // Animate intermediate lines:
  var min, max, dir;
  if (src &gt; dst) {
    min = dst + 1;
    max = src;
    dir = 1;
  } else {
    min = src;
    max = dst - 1;
    dir = -1;
  }
  var count = max - min + 1;
  var mid = (count - 1) / 2;
  line.filter(function(d, i) { return i &gt;= min &amp;&amp; i &lt;= max; })
    .transition()
      .duration(v_animate/3)
      .delay(function(d, i) { return v_animate/3 + v_animate/6 * (i - mid) / mid * -dir; })
      .attr(&quot;transform&quot;, function(d, i) { return &quot;translate(&quot; + v_x(min+i) + &quot;, &quot; + v_height + &quot;)&quot;; });
}

function chart(stage1, stage2) {
  var m = v_n;

  // Combat disqus script duplication.
  if (d3.selectAll(&quot;svg#chart1&quot;)[0].length != 0) {
    return;
  }

  var svg = d3.select(&quot;div#chart&quot;).append(&quot;svg&quot;)
      .attr(&quot;width&quot;, v_width + v_margin.left + v_margin.right)
      .attr(&quot;height&quot;, v_height + v_margin.top + v_margin.bottom)
      .attr(&quot;id&quot;, &quot;chart1&quot;)
      .style(&quot;margin-left&quot;, v_margin.left + &quot;px&quot;)
      .style(&quot;margin-right&quot;, v_margin.right + &quot;px&quot;)
    .append(&quot;g&quot;)
      .attr(&quot;transform&quot;, &quot;translate(&quot; + v_margin.left + &quot;,&quot; + v_margin.top + &quot;)&quot;);

  var line = svg.selectAll(&quot;.line&quot;)
      .data(d3.range(v_n))
    .enter().append(&quot;line&quot;)
      .attr(&quot;class&quot;, &quot;line&quot;)
      .attr(&quot;x2&quot;, function(d) { return v_size * Math.sin(v_y(d)); })
      .attr(&quot;y2&quot;, function(d) { return -v_size * Math.cos(v_y(d)); })
      .attr(&quot;transform&quot;, function(d, i) { return &quot;translate(&quot; + v_x(i) + &quot;,&quot; + v_height + &quot;)&quot;; });

  var play = svg.append(&quot;g&quot;)
      .attr(&quot;class&quot;, &quot;play&quot;)
      .on(&quot;click&quot;, start);

  play.append(&quot;path&quot;)
      .attr(&quot;d&quot;, &quot;M-30,-30L30,0L-30,30Z&quot;)
      .attr(&quot;transform&quot;, &quot;translate(&quot; + v_width / 2 + &quot;,&quot; + v_height / 2 + &quot;)scale(.6)&quot;);

  play.append(&quot;rect&quot;)
      .attr(&quot;width&quot;, v_width)
      .attr(&quot;height&quot;, v_height);

  function start() {
    play.style(&quot;display&quot;, &quot;none&quot;);

    line = svg.selectAll(&quot;.line&quot;)
        .data(shuffle(d3.range(v_n)))
        .attr(&quot;x2&quot;, function(d) { return v_size * Math.sin(v_y(d)); })
        .attr(&quot;y2&quot;, function(d) { return -v_size * Math.cos(v_y(d)); })
        .attr(&quot;transform&quot;, function(d, i) { return &quot;translate(&quot; + v_x(i) + &quot;,&quot; + v_height + &quot;)&quot;; })
        .style(&quot;stroke&quot;, COLOR_IDLE);

    var pause = 0,
        pass = 1,
        stage1Data = new Object();
    var interval = setInterval(function() {
      if (pause &gt; 0) {
        pause--;
        return;
      }
      switch(pass) {
        case 1:
          if (!(m = stage1(line, stage1Data, m))) {
            m = v_n;
            pass = 2;
            pause = 2;
            // Wait for all trailing animations before running this.
            setTimeout(function() {
              line.transition()
                  .duration(v_animate*1/3)
                  .delay(function(d, i) { return (v_animate*2/3)/v_n * i })
                  .style(&quot;stroke&quot;, COLOR_IDLE)
                  .style(&quot;stroke-width&quot;, STROKE);
            }, v_tick);
          }
          break;
        case 2:
          if (!(m = stage2(line, m))) {
            m = v_n;
            pass = 3;
          }
          break;
        case 3:
          clearInterval(interval);
          setTimeout(function() {
            play.style(&quot;display&quot;, null);
            line = svg.selectAll(&quot;.line&quot;)
                .data(d3.range(v_n))
                .attr(&quot;x2&quot;, function(d) { return v_size * Math.sin(v_y(d)); })
                .attr(&quot;y2&quot;, function(d) { return -v_size * Math.cos(v_y(d)); })
                .attr(&quot;transform&quot;, function(d, i) { return &quot;translate(&quot; + v_x(i) + &quot;,&quot; + v_height + &quot;)&quot;; })
                .style(&quot;stroke&quot;, COLOR_IDLE);
          }, 5000);
          break;
      }
    }, v_tick);
  }

  return svg;
}

function stage1(line, data, m) {
  if (data.prefix == undefined) {
    // Initialize data container.
    data.prefix = null;
    data.on = 0;
    data.key = null;
  }
  if (data.key == null) {
    // Start of a new pass.
    data.prefix = ~~Math.sqrt(m-1);
    if (data.prefix &lt;= 0) {
      // stage is done.
      return 0;
    }

    // Find maximum bar from prefix, to use as the key.
    data.key = 0;
    for (var i = 1; i &lt; data.prefix; ++i) {
      if (line[0][i].__data__ &gt; line[0][data.key].__data__) {
        data.key = i;
      }
    }

    // Re-color the active region for this pass.
    line.select(function(d, i) { return i &lt; m ? this : null })
      .style(&quot;stroke&quot;, function(d, i) { return i &lt; data.prefix ? (i == data.key ? COLOR_KEY : COLOR_PARTITION) : COLOR_IDLE; })
      .style(&quot;stroke-width&quot;, function(d, i) { return i == data.key ? STROKE_BOLD : STROKE; });

    // Next entry, start scanning immediately after the prefix.
    data.on = data.prefix;
    return m;
  }

  // Mid pass: start by checking for elements at end to skip.
  while (m &gt; data.prefix &amp;&amp; line[0][m-1].__data__ &gt;= line[0][data.key].__data__) {
    d3.select(line[0][m-1])
        .style(&quot;stroke&quot;, COLOR_DONE);
    m--;
  }

  // Next, find one element from the front to swap forward.
  while (data.on &lt; m - 1 &amp;&amp; line[0][data.on].__data__ &lt; line[0][data.key].__data__) {
    data.on++;
  }
  if (data.on &gt;= m - 1) {
    // None left - pass is done.
    swap(line, data.key, m-1, COLOR_KEY_ANIMATING);
    data.key = null;
    return m-1;
  }
  swap(line, data.on, m-1);
  return m-1;
}

function stage2(line, m) {
  // Insertion sort, from right to left.
  if (m == v_n) {
    d3.select(line[0][m-1])
        .style(&quot;stroke&quot;, COLOR_DONE);
    return m-1;
  }
  while (m &gt; 0 &amp;&amp; line[0][m-1].__data__ &lt; line[0][m].__data__) {
    d3.select(line[0][m-1])
        .style(&quot;stroke&quot;, COLOR_DONE);
    m--;
  }
  if (m == 0) {
    return 0;
  }
  
  // m-1 needs to be slid forward. Find the destination:
  var dst = m;
  for (; dst &lt; line[0].length; dst++) {
    if (line[0][m-1].__data__ &lt; line[0][dst].__data__) {
      break;
    }
  }
  insert(line, m-1, dst);
  return m-1;
}

chart(stage1, stage2);

&lt;/script&gt;
&lt;p id=&quot;disclaimer&quot;&gt;



&lt;small&gt;&lt;i&gt;Disclaimer: The visualization I&#39;m building off looks great across platforms, so if there is anything wrong with this one, it&#39;s from my changes. Go see the original if you don&#39;t believe me.&lt;/i&gt;&lt;/small&gt;&lt;/p&gt;For those that don&#39;t remember the algorithm, here&#39;s a synopsis. There are two stages:
&lt;ul&gt;
&lt;li&gt;Stage 1:&lt;ol&gt;
&lt;li&gt;Take a prefix of size &amp;radic;m, where m is the size of the working set.&lt;/li&gt;
&lt;li&gt;Pick the maximum (in this case, most-right-leaning) of the prefix, and take it as the pivot.&lt;/li&gt;
&lt;li&gt;Walk through the list, pulling out anything greater than the pivot and moving it to the end of the working set.&lt;/li&gt;
&lt;li&gt;Move the pivot to the end of the working set.&lt;/li&gt;
&lt;li&gt;Go back to step 1 and repeat.
&lt;/ol&gt;&lt;/li&gt;
&lt;li&gt;Stage 2: a bog-standard insertion sort.&lt;/li&gt;
&lt;/ul&gt;As the passes proceed the pivots decrease in size and more values are matched, allowing the array to be put into a &quot;mostly sorted&quot; state. On average, every element is within &amp;radic;n spots of the correct position, which the insertion sort then corrects. Overall complexity O(n&lt;sup&gt;1.5&lt;/sup&gt;) time, O(1) extra space.


Code for the visualization is at &lt;a href=&quot;https://gist.github.com/1628602&quot;&gt;https://gist.github.com/1628602&lt;/a&gt; for easy perusing.&lt;br /&gt;
&lt;br /&gt;
Looks good to me! What do you think?</content><link rel='replies' type='application/atom+xml' href='http://www.thelowlyprogrammer.com/feeds/415822424499778135/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.thelowlyprogrammer.com/2012/01/marriage-sort-visualization.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1806360094658697411/posts/default/415822424499778135'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1806360094658697411/posts/default/415822424499778135'/><link rel='alternate' type='text/html' href='http://www.thelowlyprogrammer.com/2012/01/marriage-sort-visualization.html' title='Marriage Sort, a visualization'/><author><name>Eric Burnett</name><uri>http://www.blogger.com/profile/10741882872804697111</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1806360094658697411.post-39495923321889634</id><published>2011-05-10T23:53:00.002-04:00</published><updated>2021-07-17T22:23:26.778-04:00</updated><title type='text'>The Game of Life, part 2: HashLife</title><content type='html'>&lt;p&gt;&lt;a href=&quot;http://www.thelowlyprogrammer.com/2011/03/game-of-life-part-1.html&quot;&gt;&lt;em&gt;Last time I wrote&lt;/em&gt;&lt;/a&gt;&lt;em&gt; I gave a brief introduction to the Game of Life and a very simple Python implementation for visualizing it. I will freely admit that was a teaser post; this post gets into the real meat of the topic with an overview of the &lt;/em&gt;&lt;a href=&quot;https://en.wikipedia.org/wiki/Hashlife&quot;&gt;&lt;em&gt;HashLife&lt;/em&gt;&lt;/a&gt;&lt;em&gt; algorithm and a much more interesting implementation.&lt;/em&gt;&lt;em&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg-uhb6ASq2WYx0Q2NPGwnuCCA3dazYDSDZfMwsPZnmsX-quPGL6oB3UWUUzjOObq8Gt5mKn_d-AGHO_ZBET_jQ5zgWsSWXOWfI0j6VUVrRXaTQbnuhXkmHPg9OGkhHe1RmMn9yUWSLpJdi/s1600-h/life%252520570%25255B4%25255D.png&quot;&gt;&lt;img style=&quot;background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: block; float: none; border-top-width: 0px; border-bottom-width: 0px; margin-left: auto; border-left-width: 0px; margin-right: auto; padding-top: 0px&quot; title=&quot;life 570&quot; border=&quot;0&quot; alt=&quot;life 570&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhn-kxjSMNooFF4RYTQRyS5TnVs3TvV_OyoWCHMli1eWplfzqLZotA3PW-J3Scuc8LjEUI8NHXdMMjEAepozFgKsd6B5NdFTZnU2_QS0i1V5ZNmdQmxz3pNBdISX_1V7YMNL1-j3O4mjBzU/?imgmax=800&quot; width=&quot;590&quot; height=&quot;342&quot; /&gt;&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;&lt;h2&gt;Introduction&lt;/h2&gt;&lt;p&gt;This entry has taken me an embarrassingly long time to post. As is my habit, I wrote the code and 90% of the post, and then left it for months and months. Whoops! &lt;/p&gt;&lt;p&gt;If you haven’t played with a Game of Life viewer before they are legitimately fun to toy around with - I encourage you to check this one out (code is &lt;a href=&quot;https://github.com/EricBurnett/GameOfLife/tree/v2&quot;&gt;here&lt;/a&gt;). Since the last version everything is much improved. The viewer supports a larger set of controls (see the &lt;a href=&quot;https://github.com/EricBurnett/GameOfLife/blob/v2/README&quot;&gt;README&lt;/a&gt; for details) and basic file reading is implemented so it’s possible to try new starting patterns on the fly. And, as promised, I’ve implemented the HashLife algorithm to massively speed up iterations, so enormous patterns billions of generations forward are easily within your reach.&lt;/p&gt;&lt;h2&gt;Algorithm&lt;/h2&gt;&lt;p&gt;HashLife is a simple yet interesting algorithm. Invented in 1984 by Bill Gosper (of &lt;em&gt;Gosper glider gun&lt;/em&gt; fame), it exploits repeated patterns to dramatically cut down the work required to support large patterns over vast numbers of iterations. Between the &lt;a href=&quot;http://en.wikipedia.org/wiki/Hashlife&quot;&gt;Wikipedia page&lt;/a&gt; and the enigmatically named “&lt;a href=&quot;http://drdobbs.com/high-performance-computing/184406478&quot;&gt;An Algorithm for Compressing Space and Time&lt;/a&gt;” in Dr. Dobb’s Journal I think it’s decently well explained, but it took me a couple read-throughs to really wrap my head around so I’m going to try to give an overview of the key insights it utilizes. &lt;/p&gt;&lt;p align=&quot;center&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiTd3WPv0JttmnwIQWySruW8wW5WltW0tvuf9FLmvgliPaTaf1QmS5-5kfp3-WwLtkJha3okjcZWmfqzBrF_zu3P8bPSeEMwZTenRMxoZFKortGN83am8x6uo2Cv9m77M6hz6-nTarNO2DC/s1600-h/quadtree5.png&quot;&gt;&lt;img style=&quot;background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: right; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px&quot; title=&quot;quadtree&quot; border=&quot;0&quot; alt=&quot;quadtree&quot; align=&quot;right&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhjIaG-EnwXLsCqIo_pCffD24TcrNx1r_12NeHlt8pz3pr40ZO5UH2oW-p6tRYpeYLzYVu03wYrarJUYebucWyKgbdcdpCypTnzDt0dSaGNuUDKhjw9kVvgmAPHq6PMzFrjfZ7nd9sWEFqR/?imgmax=800&quot; width=&quot;240&quot; height=&quot;240&quot; /&gt;&lt;/a&gt;&lt;/p&gt;&lt;p&gt;At it’s heart, HashLife is built around the concept of a quadtree. If you’re unfamiliar with it, a quadtree takes a square region and breaks it into four quadrants, each a quarter the size of the original. Each quadrant is further broken down into quadrants of its own, and on down. At the bottom, in squares of some minimum size like 2x2, actual points are stored. This structure is usually used to make spatial queries like “what points intersect this bounding box” efficient, but in this case two other properties are taken advantage of. First, nodes at any level are uniquely defined by the points within their region, which means duplicated regions can be backed by the same node in memory. For the Game of Life, where there are repeated patterns and empty regions galore, this can drastically reduce the space required. Second, in the Game of Life a square of&amp;#160; (n)x(n) points fully dictates the inner (n-2)x(n-2) core one generation forward, the inner (n/2)x(n/2) core n/4 generations forward, irrespective of what cells are adjacent to it. So the future core of a node can be calculated once and will apply at any future point in time, anywhere in the tree. &lt;/p&gt;&lt;p&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiDhI_LF4Ng6m_AWt8qubn86mr62EUrHB63Wmo8hRrU-tsHA272Q6In2l9qw_df-HQXFm3kNQYjmJjgGLQAU1YSm7W63v-_FamXdb-lTSTczEBgsomh-uoZMAT4QGWpjIJF5eStqNLyD1u9/s1600-h/Inner%252520nodes%25255B6%25255D.png&quot;&gt;&lt;img style=&quot;background-image: none; border-right-width: 0px; margin: 10px auto; padding-left: 0px; padding-right: 0px; display: block; float: none; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px&quot; title=&quot;Inner nodes&quot; border=&quot;0&quot; alt=&quot;Inner nodes&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiXh_hu4Y-3UAzG1gQ0dEeyk4GHNmsgfKmcZ8Zkvb8FLqxNpnu8pJmAgDralZMsxceQ4lvVjjnJEj2jFMKbGDFKuw0GiWDu_bJt4IGw0ComQl3wiSs8BFTqDdwjMotpE2La5SncCpqnWpQG/?imgmax=800&quot; width=&quot;404&quot; height=&quot;111&quot; /&gt;&lt;/a&gt;Together these properties allow for ridiculous speedups. Hashing and sharing nodes drastically reduces the space requirements, with exponentially more sharing the further down the tree you go. There are only 16 possible leaf nodes, after all! From this, calculating the future core for a node requires exponentially less time than a naïve implementation would. It can be done by recursively calculating the inner core of smaller nodes, where the better caching comes into play, and then combining them together into a new node. You might be wondering if the gains from caching are lost to the increasing difficulty of determining which nodes are equal, but with a couple careful invariants we actually get that for free. First, nodes must be immutable - this one’s pretty straightforward. Second, nodes must be unique at all times. This forces us to build the tree from the bottom up, but then checking if a new node duplicates an existing one is simply a matter of checking if there are any existing nodes that point to the same set of quadrants in the same order, a problem that hash tables trivially solve. &lt;/p&gt;&lt;div style=&quot;padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px&quot; id=&quot;scid:f32c3428-b7e9-4f15-a8ea-c502c7ff2e88:f9a9f18d-25ee-40e2-a10b-2ed4abf31dc8&quot; class=&quot;wlWriterEditableSmartContent&quot;&gt;&lt;pre class=&quot;brush: python;&quot;&gt;def __hash__(self):
# Hash is dependent on cells only, not e.g. _next.
# Required for Canonical(), so cannot be simply the id of the current
# object (which would otherwise work).
return hash((id(self._nw), id(self._ne), id(self._sw), id(self._se)))

def __eq__(self, other):
&quot;&quot;&quot;Are two nodes equal? Doesn&#39;t take caching _next into account.&quot;&quot;&quot;
if id(self) == id(other):
return True
return (id(self._nw) == id(other._nw) and
id(self._ne) == id(other._ne) and
id(self._sw) == id(other._sw) and
id(self._se) == id(other._se))&lt;/pre&gt;&lt;/div&gt;&lt;h2&gt;Implementation&lt;/h2&gt;&lt;p&gt;As before, the code I’ve written is for Python 2.6 and makes use of PyGame, although neither dependency is terribly sticky. The code lives in a &lt;a href=&quot;https://github.com/EricBurnett/GameOfLife/tree/v2&quot;&gt;repository on github&lt;/a&gt;, and I welcome any contributions you care to make. As the code here is complicated enough to be almost guaranteed a bug or two, there is a basic set of unit tests in life_test.py and the code itself is liberally sprinkled with asserts. Incidentally, removing the asserts nets a 20% performance gain (as measured by the time it takes to run the ‘PerformanceTest’ unit test), although I find the development time saved by having them is easily worth keeping them in forever. As noted later, the performance of the implementation isn’t all that important anyways. Which is a good thing, since I coded it in Python! &lt;/p&gt;&lt;p&gt;A comment on rewrites: during the transition from version 1 - a simple brute force algorithm - to version 2 - the Node class that implements HashLife - I had both algorithms implemented in parallel for a while. This let me have every second frame rendered by the old algorithm so I could ensure that at different times and different render speeds that the algorithms were coming up with the same results. I’ve seen this pattern used at work for migrating to replacement systems and it’s very much worth the extra glue code you have to write or the confidence it gives. John Carmack recently &lt;a href=&quot;http://altdevblogaday.com/2011/11/22/parallel-implementations/&quot;&gt;wrote about parallel implementations&lt;/a&gt; on his own blog, if you want to hear more on the topic.&lt;/p&gt;&lt;h2&gt;Performance&lt;/h2&gt;&lt;p&gt;The performance is hard to objectively detail for an algorithm like this. For example, it takes ~1 second to generate the billionth generation of the &lt;a href=&quot;http://www.conwaylife.com/wiki/index.php?title=Backrake_3&quot;&gt;backrake 3&lt;/a&gt; pattern, which has around 300,000,000 live cells; it takes ~2 seconds to generate the quintillionth generation with 3x10^17 live cells. But this is a perfect patterns to showcase HashLife - a simple spaceship traveling in a straight line, generating a steady stream of gliders. In comparison, a chaotic pattern like &lt;a href=&quot;http://www.conwaylife.com/wiki/index.php?title=Acorn&quot;&gt;Acorn &lt;/a&gt;takes almost 25 seconds to generate just 5000 generations with at most 1057 alive at any time. As it stands the properties of the algorithm drastically outweigh the peculiarities of the implementation for anything I care to do. Although I must say, if you want to compare it to another implementation in an apples to apples comparison I’d love to hear the numbers you get.&lt;/p&gt;&lt;p&gt;As always, I’d love to hear what you think! &lt;/p&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.thelowlyprogrammer.com/feeds/39495923321889634/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.thelowlyprogrammer.com/2011/05/game-of-life-part-2-hashlife.html#comment-form' title='5 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1806360094658697411/posts/default/39495923321889634'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1806360094658697411/posts/default/39495923321889634'/><link rel='alternate' type='text/html' href='http://www.thelowlyprogrammer.com/2011/05/game-of-life-part-2-hashlife.html' title='The Game of Life, part 2: HashLife'/><author><name>Eric Burnett</name><uri>http://www.blogger.com/profile/10741882872804697111</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhn-kxjSMNooFF4RYTQRyS5TnVs3TvV_OyoWCHMli1eWplfzqLZotA3PW-J3Scuc8LjEUI8NHXdMMjEAepozFgKsd6B5NdFTZnU2_QS0i1V5ZNmdQmxz3pNBdISX_1V7YMNL1-j3O4mjBzU/s72-c?imgmax=800" height="72" width="72"/><thr:total>5</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1806360094658697411.post-936349929642113617</id><published>2011-03-12T11:03:00.003-05:00</published><updated>2021-07-17T22:48:46.335-04:00</updated><title type='text'>The Game of Life, part 1</title><content type='html'>&lt;p&gt;&lt;i&gt;Update: See &lt;a href=&quot;http://www.thelowlyprogrammer.com/2011/05/game-of-life-part-2-hashlife.html&quot;&gt;part 2&lt;/a&gt; for the implemented HashLife algorithm.&lt;/i&gt;&lt;br /&gt;
&lt;br /&gt;
The &lt;a title=&quot;Conway&amp;#39;s Game of Life - Wikipedia&quot; href=&quot;https://en.wikipedia.org/wiki/Conway%27s_Game_of_Life&quot;&gt;Game of Life&lt;/a&gt; is a fascinating system. It was invented by &lt;a title=&quot;John Horton Conway - Wikipedia&quot; href=&quot;https://en.wikipedia.org/wiki/John_Horton_Conway&quot;&gt;John Conway&lt;/a&gt; in 1970 and has been studied continuously ever since. For those reading who haven’t heard of it before, a brief explanation: The world is an infinite grid of points, all either alive or dead. After each generation – or ‘iteration’ if you’d prefer – cells are updated according to the following three rules:&lt;/p&gt;&lt;ol&gt;&lt;li&gt;If a cell is alive and it has two or three live neighbours, it stays alive. &lt;/li&gt;
&lt;li&gt;If a cell is dead and it has exactly three live neighbours, it becomes alive (tripartite reproduction?). &lt;/li&gt;
&lt;li&gt;Any other cell is dead. &lt;/li&gt;
&lt;/ol&gt;&lt;p align=&quot;center&quot;&gt;&lt;table border=&quot;0&quot; cellspacing=&quot;30&quot; cellpadding=&quot;2&quot; width=&quot;587&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;         &lt;td valign=&quot;top&quot; width=&quot;210&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjzrCcbFs9UKjyFTAIk-9qtjYgbS12aAQxPgoZW-qhYTc-1uimK8Ozi6ofHh-GVg-I73cehp_MDg-QkmI0ytf-5oQuIoe0s7zVo_NHCB2e4Zgt4wG9TrrtiXhARXKOEockmP-qDA7W48mw4/s1600-h/Blinker%5B6%5D.png&quot;&gt;&lt;img style=&quot;background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px&quot; title=&quot;Blinker&quot; border=&quot;0&quot; alt=&quot;Blinker&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgI4RzFKAE6CRsr8HDbCdfIPEVbwrtrc66z7277kbfz2roMObjRmHnN-TiftYe7omwUXKBvUbLtpGuFbWsrnnnYhF-J6jBftzTRctzsMmVXFFCximffgqsrqv0sBdKVZMv7gfCpXAFU0HYG/?imgmax=800&quot; width=&quot;203&quot; height=&quot;60&quot; /&gt;&lt;/a&gt;&lt;/td&gt;          &lt;td valign=&quot;top&quot; width=&quot;67&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgMzcrZ2FxjjNw0WEkqYctgoaOLdyq9kb4linTwCcVugEzNDus62RoWzyoS2I4nMguIbfBJEsuQfkiZb3l_vD_hS4M_N42i52ZwlQZ_Bo6bQlPx4oAmUuaMm41eaCmpceLz93JDeOo0C_SA/s1600-h/infinite%20growth%5B4%5D.png&quot;&gt;&lt;img style=&quot;background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px&quot; title=&quot;infinite growth&quot; border=&quot;0&quot; alt=&quot;infinite growth&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg3ZppwdbZDxJ3ZNhW6Ug8Kths29VX9ksQ_iKo4K5sI_Uv2UAl6gg-VF74yLX7RI301bcBBZP_hvKZer9pPzzvLzEEGjBrbZ67jbciRf1H6rEstxTBhIGZddmYyB88mIdp4BvvmNcEk38K1/?imgmax=800&quot; width=&quot;60&quot; height=&quot;60&quot; /&gt;&lt;/a&gt;&lt;/td&gt;          &lt;td valign=&quot;top&quot; width=&quot;210&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhDpfGpVN-tge33u4v4UquYHFmmeLnMZSbFRz9JZuJhZ0pSqtyhHMHXRGJ5lSmS1d-KW6ksl8O2BT9Pac5HvU_x4JMDjtt9QZn0wDeIYRcjf5LPK8qY5TpNUAGO2i9YlO34uJhu3Wv4wEnx/s1600-h/Glider%5B4%5D.png&quot;&gt;&lt;img style=&quot;background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px&quot; title=&quot;Glider&quot; border=&quot;0&quot; alt=&quot;Glider&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEguEHrIWHG5e0elr9Wt5dHPmc9JphP-bywS5abO83Cmuz3WwBuO_SS7bjec6ojO7c_a1uCwi7KbhDI48VI9AfcFdsWHSTRAWS-wiRir8p_17OwNmA5g68EXwcIBZWT_GKj9zyfD6uj6_kiZ/?imgmax=800&quot; width=&quot;203&quot; height=&quot;60&quot; /&gt;&lt;/a&gt;&lt;/td&gt;       &lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;&lt;/p&gt;&lt;p&gt;From these simple rules amazing complexity can arise. Some configurations are stable, like the period two “blinker” [above left], or the period four “glider” [above right] that moves one row over and one row down with every cycle. Other configurations, like the one above centre, grow infinitely – this one spits out two gliders then lays down a zig-zag strip of blocks forever after. &lt;/p&gt;&lt;p&gt;There is more to the Game of Life than pretty patterns and curious growth, I must hasten to add. It has been studied by a host of people in a variety of fields and has gone on to start a new branch of mathematics (&lt;a title=&quot;Cellular automaton - Wikipedia&quot; href=&quot;https://en.wikipedia.org/wiki/Cellular_automaton&quot;&gt;cellular automata&lt;/a&gt;) and spur discussions on whether a &lt;a title=&quot;Emergence - Wikipedia&quot; href=&quot;https://en.wikipedia.org/wiki/Emergence&quot;&gt;sufficiently complicated pattern&lt;/a&gt; could be considered intelligent. It has also been proven to be &lt;a title=&quot;Life Universal Computer&quot; href=&quot;http://www.igblan.free-online.co.uk/igblan/ca/&quot;&gt;Turing complete&lt;/a&gt;, so any computation your computer can run can be run by simulating the Game of Life with the correct starting state.&lt;/p&gt;&lt;p&gt;I have implemented a basic python program for simulating the Game of Life &lt;a title=&quot;EricBurnett/GameOfLife - GitHub&quot; href=&quot;https://github.com/EricBurnett/GameOfLife/tree/v1&quot;&gt;on GitHub&lt;/a&gt;. It allows for infinite patterns, grows the field of view automatically, and allows speed to be controlled with Up/Down, but otherwise is a very simple implementation. The goal here is to eventually implement some of the more interesting algorithms for speeding up the simulation.&amp;#160; There are numerous such algorithms, although the one I find the most interesting is called &lt;a title=&quot;An Algorithm for Compressing Space and Time&quot; href=&quot;http://www.drdobbs.com/high-performance-computing/184406478&quot;&gt;Hashlife&lt;/a&gt; and exploits repeated patterns through space and time to achieve an exponential speedup in running the simulation. More details in part 2, whenever I write it :).&lt;/p&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.thelowlyprogrammer.com/feeds/936349929642113617/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.thelowlyprogrammer.com/2011/03/game-of-life-part-1.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1806360094658697411/posts/default/936349929642113617'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1806360094658697411/posts/default/936349929642113617'/><link rel='alternate' type='text/html' href='http://www.thelowlyprogrammer.com/2011/03/game-of-life-part-1.html' title='The Game of Life, part 1'/><author><name>Eric Burnett</name><uri>http://www.blogger.com/profile/10741882872804697111</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgI4RzFKAE6CRsr8HDbCdfIPEVbwrtrc66z7277kbfz2roMObjRmHnN-TiftYe7omwUXKBvUbLtpGuFbWsrnnnYhF-J6jBftzTRctzsMmVXFFCximffgqsrqv0sBdKVZMv7gfCpXAFU0HYG/s72-c?imgmax=800" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1806360094658697411.post-7720975805796492880</id><published>2010-06-08T02:25:00.000-04:00</published><updated>2010-06-08T02:25:15.053-04:00</updated><title type='text'>New Job!</title><content type='html'>Today I officially started at Google! Exciting stuff. I will be working on the DoubleClick Ad Exchange doing something-or-other for the time being — however as I am neither a senior engineer nor a corporate figurehead, I do not plan to write about my work. This should, however, explain my recent absence :).
&lt;br /&gt;&lt;br /&gt;
That is all.</content><link rel='replies' type='application/atom+xml' href='http://www.thelowlyprogrammer.com/feeds/7720975805796492880/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.thelowlyprogrammer.com/2010/06/new-job.html#comment-form' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1806360094658697411/posts/default/7720975805796492880'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1806360094658697411/posts/default/7720975805796492880'/><link rel='alternate' type='text/html' href='http://www.thelowlyprogrammer.com/2010/06/new-job.html' title='New Job!'/><author><name>Eric Burnett</name><uri>http://www.blogger.com/profile/10741882872804697111</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1806360094658697411.post-8001471963766584930</id><published>2010-04-28T14:23:00.015-04:00</published><updated>2021-07-17T22:48:00.689-04:00</updated><title type='text'>Robustly hot swapping binaries (with code)</title><content type='html'>&lt;table border=&quot;1&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;       &lt;td style=&quot;background-color: black; color: rgb(200,200,200)&quot;&gt;         &lt;pre&gt;Going down for swap
Coming up from swap!
This is generation 2 running 0.2 software (initial version 0.1)
Running state machine&lt;/pre&gt;

&lt;/td&gt;
&lt;/tr&gt;

&lt;/tbody&gt;&lt;/table&gt;

&lt;br/&gt;
&lt;p&gt;
&amp;#160; 
A while ago I remember reading an article by Nathan Wiegand on &lt;a href=&quot;http://nathanwiegand.com/wp/2010/02/hot-swapping-binaries/&quot;&gt;hot swapping binaries&lt;/a&gt;. This was a very eye-opening article for me – before reading it, hot swapping was one of those black arts I never really thought about, and certainly wouldn’t have thought was at all &lt;em&gt;easy.&lt;/em&gt; I highly recommend you read it for yourself. Go ahead, I’ll wait. &lt;/p&gt;


&lt;p&gt;
There. Did you notice it? The elephant in the room? One thing the article doesn’t address is how to design programs to &lt;em&gt;use&lt;/em&gt; this fancy new ability, without being fragile and crashing and all those bad things. I’ve been mulling this over since reading it, and have settled on a basic design that I’ll present here. But don’t worry, this isn’t &lt;em&gt;just &lt;/em&gt;a thought design…I’ve actually coded it up and made sure it works as expected. Feel free to &lt;a href=&quot;#code&quot;&gt;jump down&lt;/a&gt; and check it out before reading the design. Still, caveat emptor and all that.&lt;/p&gt;


&lt;h2&gt;
Design Goals&lt;/h2&gt;


&lt;p&gt;
For this design, I am focusing on three key goals:&lt;/p&gt;


&lt;ul&gt;
&lt;li&gt;Allow updating to any future version &lt;/li&gt;


&lt;li&gt;Allow updating to any &lt;em&gt;previous&lt;/em&gt; version &lt;/li&gt;


&lt;li&gt;Make it easy to be crash-free &lt;/li&gt;

&lt;/ul&gt;


&lt;p&gt;
Simple enough? Updating forward is a pretty obvious goal, as is crash-free code. I want to allow updating backwards as well for the simple expedient that I don’t expect all new code to be bug free, and so it might be desirable to roll back when a bug is introduced.&lt;/p&gt;


&lt;h2&gt;
&lt;/h2&gt;


&lt;h2&gt;
Design&lt;/h2&gt;


&lt;p&gt;
To achieve this, I’ve settled on a shortlist of constraints for the code:&lt;/p&gt;


&lt;ol&gt;
&lt;li&gt;Use protocol buffers to store all state &lt;/li&gt;


&lt;li&gt;Provide suitable defaults for everything, and be robust against weird state &lt;/li&gt;


&lt;li&gt;Structure the code as a state machine &lt;/li&gt;

&lt;/ol&gt;


&lt;p&gt;
I did say the list was &lt;em&gt;short&lt;/em&gt;. Let’s look at these in detail:&lt;/p&gt;


&lt;ol&gt;
&lt;li&gt;Protocol buffers are an obvious choice for persisting state, as they are forward and backward compatible by design. Care must be taken to not re-use tag numbers and to never add ‘required’ fields, but this is an easy requirement to satisfy. Now, using protocol buffers to store&lt;em&gt; everything&lt;/em&gt; does incur some overhead, but they are quite efficient by design and we really only need to store all state between state machine transitions so local variables are still quick. &lt;/li&gt;


&lt;li&gt;Hand in hand with 1), we cannot always expect to have the data we want in the format we want version to version. To accommodate this we must pick suitable defaults for fields, and if necessary be able to get new values at runtime. At the same time, if the meaning or format of a field is changing, it is probably better to use a new field. We will always try to handle weird data that may show up, but this shouldn’t be abused. &lt;/li&gt;


&lt;li&gt;Finally, structure the code as a state machine. This surfaces all state machine transitions as potential points for upgrading versions, and forces state to be in the protocol buffer when these transitions are crossed to ensure important data isn’t forgotten. And like everything else, the next state data can be stored in the protocol buffer. &lt;/li&gt;

&lt;/ol&gt;


&lt;p&gt;
There is one problem with 3), however. What happens when new states are added? Going forward is easy, but if we update to a previous version when we’re in one of these new states, it will have no idea where to start running again. We could try storing fallback states or something like that, but that seems too fragile. Instead, I would recommend not allowing updates to occur when transitioning to these new states. Then, a few versions down the line when you’re sure you won’t need to downgrade past where they were added, remove that restriction.&lt;/p&gt;


&lt;pre style=&quot;border-bottom: #000000 1px solid; border-left: #000000 1px solid; padding-bottom: 5px; background-color: #c0c0c0; min-height: 40px; padding-left: 5px; width: 590px; padding-right: 5px; overflow: auto; border-top: #000000 1px solid; border-right: #000000 1px solid; padding-top: 5px&quot;&gt;&lt;pre style=&quot;background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px&quot;&gt;enum State {
&lt;/pre&gt;&lt;pre style=&quot;background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px&quot;&gt;    // Special states supported by the program infrastructure.
&lt;/pre&gt;&lt;pre style=&quot;background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px&quot;&gt;    STATE_NONE  = 0;
&lt;/pre&gt;&lt;pre style=&quot;background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px&quot;&gt;    STATE_DONE  = 1;
&lt;/pre&gt;&lt;pre style=&quot;background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px&quot;&gt;    STATE_ERROR = 2;
&lt;/pre&gt;
&lt;pre style=&quot;background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px&quot;&gt;&lt;/pre&gt;&lt;pre style=&quot;background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px&quot;&gt;    // Program states. Unknown state transitions lead to ERROR and terminate the
&lt;/pre&gt;&lt;pre style=&quot;background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px&quot;&gt;    // program, so should be avoided at all costs.
&lt;/pre&gt;&lt;pre style=&quot;background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px&quot;&gt;    STATE_INIT          = 3;
&lt;/pre&gt;&lt;pre style=&quot;background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px&quot;&gt;    STATE_PROCESS_LINE  = 4;
&lt;/pre&gt;&lt;pre style=&quot;background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px&quot;&gt;    STATE_MUTATE_LINE   = 5;
&lt;/pre&gt;&lt;pre style=&quot;background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px&quot;&gt;  }
&lt;/pre&gt;
&lt;pre style=&quot;background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px&quot;&gt;&lt;/pre&gt;&lt;pre style=&quot;background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px&quot;&gt;  optional State prev_state = 2 [default = STATE_NONE];
&lt;/pre&gt;&lt;pre style=&quot;background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px&quot;&gt;  optional State cur_state  = 3 [default = STATE_ERROR];
&lt;/pre&gt;&lt;pre style=&quot;background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px&quot;&gt;&lt;/pre&gt;&lt;/pre&gt;


&lt;h2&gt;
What About Threads?&lt;/h2&gt;


&lt;p&gt;
You may have noticed that this design is inherently single-threaded. Threading can be added easily enough if the main thread owns all the work, and can easily and safely wait for or cancel all worker threads without losing anything. In that case, spin down the workers when you’re about to swap, and spin them up again when it completes. If your program doesn’t fit that description, however, this design may not be for you.&lt;/p&gt;


&lt;h2&gt;
Testing?&lt;/h2&gt;


&lt;p&gt;
Of course! I would recommend trying all transitions on a test instance first before upgrading the real process. You could also build in consistency checks that auto-revert if the state doesn’t meet expectations, regression tests for certain upgrade patterns, etc. This design is meant to make it easy to hot swap successfully, but it is no silver bullet.&lt;/p&gt;


&lt;h2&gt;
&lt;a name=&quot;code&quot;&gt;&lt;/a&gt;Let&#39;s See the Code!&lt;/h2&gt;


&lt;p&gt;
As always, the code is up on &lt;a href=&quot;https://github.com/EricBurnett/hotswap&quot;&gt;GitHub&lt;/a&gt; for you to peruse. It is broken into two demonstration applications, ‘v1’ and ‘v2’, that can be swapped between at will. While looping they respond to ‘u’ and ‘q’ (&lt;strong&gt;update&lt;/strong&gt; and &lt;strong&gt;quit&lt;/strong&gt;), although at times you may be prompted for other input. the makefiles build to the same target location, so build whichever one you want run next and press ‘u’ to swap to it.&lt;/p&gt;


&lt;p&gt;
The code is structured so you can use it as a framework to play with yourself easily enough. You should only need to write an init method, update the state machine and .proto file, and write the respective state methods to do the real work. The state machine and state methods will look something like this: 

&lt;/p&gt;


&lt;pre style=&quot;border-bottom: #000000 1px solid; border-left: #000000 1px solid; padding-bottom: 5px; background-color: #c0c0c0; min-height: 40px; padding-left: 5px; width: 590px; padding-right: 5px; overflow: auto; border-top: #000000 1px solid; border-right: #000000 1px solid; padding-top: 5px&quot;&gt;&lt;pre style=&quot;background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px&quot;&gt;ReturnCode runStateMachine(ProgramState&amp;amp; state) {
&lt;/pre&gt;&lt;pre style=&quot;background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px&quot;&gt;    cerr &amp;lt;&amp;lt; &amp;quot;&lt;span style=&quot;color: #8b0000&quot;&gt;Running state machine\n&lt;/span&gt;&amp;quot;;
&lt;/pre&gt;
&lt;pre style=&quot;background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px&quot;&gt;&lt;/pre&gt;&lt;pre style=&quot;background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px&quot;&gt;    &lt;span style=&quot;color: #008000&quot;&gt;// Put stdin into non-blocking, raw mode, so we can watch for character&lt;/span&gt;
&lt;/pre&gt;&lt;pre style=&quot;background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px&quot;&gt;    &lt;span style=&quot;color: #008000&quot;&gt;// input one keypress at a time.&lt;/span&gt;
&lt;/pre&gt;&lt;pre style=&quot;background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px&quot;&gt;    setStdinBlocking(&lt;span style=&quot;color: #0000ff&quot;&gt;false&lt;/span&gt;);
&lt;/pre&gt;
&lt;pre style=&quot;background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px&quot;&gt;&lt;/pre&gt;&lt;pre style=&quot;background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px&quot;&gt;    &lt;span style=&quot;color: #0000ff&quot;&gt;while&lt;/span&gt; (&lt;span style=&quot;color: #0000ff&quot;&gt;true&lt;/span&gt;) {
&lt;/pre&gt;&lt;pre style=&quot;background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px&quot;&gt;        ProgramState::State next;
&lt;/pre&gt;&lt;pre style=&quot;background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px&quot;&gt;        &lt;span style=&quot;color: #0000ff&quot;&gt;switch&lt;/span&gt; (state.cur_state()) {
&lt;/pre&gt;&lt;pre style=&quot;background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px&quot;&gt;            &lt;span style=&quot;color: #0000ff&quot;&gt;case&lt;/span&gt; ProgramState::STATE_INIT:
&lt;/pre&gt;&lt;pre style=&quot;background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px&quot;&gt;                next = runState_init(state);
&lt;/pre&gt;&lt;pre style=&quot;background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px&quot;&gt;                &lt;span style=&quot;color: #0000ff&quot;&gt;break&lt;/span&gt;;
&lt;/pre&gt;
&lt;pre style=&quot;background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px&quot;&gt;&lt;/pre&gt;&lt;pre style=&quot;background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px&quot;&gt;            &lt;span style=&quot;color: #0000ff&quot;&gt;case&lt;/span&gt; ProgramState::STATE_PROCESS_LINE:
&lt;/pre&gt;&lt;pre style=&quot;background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px&quot;&gt;                next = runState_process_line(state);
&lt;/pre&gt;&lt;pre style=&quot;background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px&quot;&gt;                &lt;span style=&quot;color: #0000ff&quot;&gt;break&lt;/span&gt;;
&lt;/pre&gt;
&lt;pre style=&quot;background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px&quot;&gt;&lt;/pre&gt;&lt;pre style=&quot;background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px&quot;&gt;            &lt;span style=&quot;color: #0000ff&quot;&gt;case&lt;/span&gt; ProgramState::STATE_DONE:
&lt;/pre&gt;&lt;pre style=&quot;background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px&quot;&gt;                setStdinBlocking(&lt;span style=&quot;color: #0000ff&quot;&gt;true&lt;/span&gt;);
&lt;/pre&gt;&lt;pre style=&quot;background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px&quot;&gt;                &lt;span style=&quot;color: #0000ff&quot;&gt;return&lt;/span&gt; SUCCESS;
&lt;/pre&gt;
&lt;pre style=&quot;background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px&quot;&gt;&lt;/pre&gt;&lt;pre style=&quot;background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px&quot;&gt;            &lt;span style=&quot;color: #0000ff&quot;&gt;case&lt;/span&gt; ProgramState::STATE_NONE:
&lt;/pre&gt;&lt;pre style=&quot;background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px&quot;&gt;            &lt;span style=&quot;color: #0000ff&quot;&gt;case&lt;/span&gt; ProgramState::STATE_ERROR:
&lt;/pre&gt;&lt;pre style=&quot;background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px&quot;&gt;            &lt;span style=&quot;color: #0000ff&quot;&gt;default&lt;/span&gt;:
&lt;/pre&gt;&lt;pre style=&quot;background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px&quot;&gt;                setStdinBlocking(&lt;span style=&quot;color: #0000ff&quot;&gt;true&lt;/span&gt;);
&lt;/pre&gt;&lt;pre style=&quot;background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px&quot;&gt;                &lt;span style=&quot;color: #0000ff&quot;&gt;return&lt;/span&gt; FAILURE;
&lt;/pre&gt;&lt;pre style=&quot;background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px&quot;&gt;        }
&lt;/pre&gt;&lt;pre style=&quot;background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px&quot;&gt;&lt;/pre&gt;
&lt;pre style=&quot;background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px&quot;&gt;        ProgramState::State cur = state.cur_state();
&lt;/pre&gt;&lt;pre style=&quot;background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px&quot;&gt;        state.set_prev_state(cur);
&lt;/pre&gt;&lt;pre style=&quot;background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px&quot;&gt;        state.set_cur_state(next);
&lt;/pre&gt;&lt;pre style=&quot;background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px&quot;&gt;&lt;/pre&gt;
&lt;pre style=&quot;background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px&quot;&gt;        &lt;span style=&quot;color: #008000&quot;&gt;// For now, simply let the user decide when to swap and quit. We can&lt;/span&gt;
&lt;/pre&gt;&lt;pre style=&quot;background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px&quot;&gt;        &lt;span style=&quot;color: #008000&quot;&gt;// always change this later.&lt;/span&gt;
&lt;/pre&gt;&lt;pre style=&quot;background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px&quot;&gt;        ReturnCode code = checkForUserSignal();
&lt;/pre&gt;&lt;pre style=&quot;background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px&quot;&gt;        &lt;span style=&quot;color: #0000ff&quot;&gt;if&lt;/span&gt; (code != CONTINUE) {
&lt;/pre&gt;&lt;pre style=&quot;background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px&quot;&gt;            setStdinBlocking(&lt;span style=&quot;color: #0000ff&quot;&gt;true&lt;/span&gt;);
&lt;/pre&gt;&lt;pre style=&quot;background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px&quot;&gt;            &lt;span style=&quot;color: #0000ff&quot;&gt;return&lt;/span&gt; code;
&lt;/pre&gt;&lt;pre style=&quot;background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px&quot;&gt;        }
&lt;/pre&gt;&lt;pre style=&quot;background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px&quot;&gt;    }
&lt;/pre&gt;&lt;pre style=&quot;background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px&quot;&gt;}
&lt;/pre&gt;&lt;pre style=&quot;background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px&quot;&gt;&lt;/pre&gt;
&lt;pre style=&quot;background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px&quot;&gt;ProgramState::State runState_init(ProgramState&amp;amp; state) {
&lt;/pre&gt;&lt;pre style=&quot;background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px&quot;&gt;    cout &amp;lt;&amp;lt; &amp;quot;&lt;span style=&quot;color: #8b0000&quot;&gt;Please provide a line of text for me to repeat ad-nauseum\n&lt;/span&gt;&amp;quot;;
&lt;/pre&gt;&lt;pre style=&quot;background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px&quot;&gt;    &lt;span style=&quot;color: #0000ff&quot;&gt;string&lt;/span&gt; line;
&lt;/pre&gt;&lt;pre style=&quot;background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px&quot;&gt;    setStdinBlocking(&lt;span style=&quot;color: #0000ff&quot;&gt;true&lt;/span&gt;);
&lt;/pre&gt;&lt;pre style=&quot;background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px&quot;&gt;    getline(cin, line);
&lt;/pre&gt;&lt;pre style=&quot;background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px&quot;&gt;    setStdinBlocking(&lt;span style=&quot;color: #0000ff&quot;&gt;false&lt;/span&gt;);
&lt;/pre&gt;&lt;pre style=&quot;background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px&quot;&gt;    state.set_line_text(line);
&lt;/pre&gt;&lt;pre style=&quot;background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px&quot;&gt;    cout &amp;lt;&amp;lt; &amp;quot;&lt;span style=&quot;color: #8b0000&quot;&gt;Thanks!\n&lt;/span&gt;&amp;quot;;
&lt;/pre&gt;&lt;pre style=&quot;background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px&quot;&gt;    state.set_line_count(0);
&lt;/pre&gt;&lt;pre style=&quot;background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px&quot;&gt;&lt;/pre&gt;
&lt;pre style=&quot;background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px&quot;&gt;    &lt;span style=&quot;color: #0000ff&quot;&gt;return&lt;/span&gt; ProgramState::STATE_PROCESS_LINE;
&lt;/pre&gt;&lt;pre style=&quot;background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px&quot;&gt;}
&lt;/pre&gt;&lt;pre style=&quot;background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px&quot;&gt;&lt;/pre&gt;&lt;/pre&gt;


&lt;p&gt;
Easy, right? And here is an example transcript from going forward and then back between the versions in the repository (behind the scenes compiles not shown):&lt;/p&gt;


&lt;table border=&quot;1&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;
&lt;td style=&quot;background-color: black; color: rgb(200,200,200)&quot;&gt;
&lt;pre&gt;eric:~/code/hotswap/v1$ &lt;span style=&quot;color:green; margin:0em&quot;&gt;../bin/hotswap.out&lt;/span&gt;
HotSwap example started - version 0.1
Initial call
Running state machine
Please provide a line of text for me to repeat ad-nauseum
&lt;span style=&quot;color:green; margin:0em&quot;&gt;All work and no play makes jack a dull boy&lt;/span&gt;
Thanks!
0: All work and no play makes jack a dull boy
1: All work and no play makes jack a dull boy
2: All work and no play makes jack a dull boy
&lt;span style=&quot;color:green; margin:0em&quot;&gt;u&lt;/span&gt;
Going down for swap
Coming up from swap!
This is generation 2 running 0.2 software (initial version 0.1)
Running state machine
3 mutations: All work and no play makes jack a dull boy
4 mutations: All work and no play maXes jack a dull boy
5 mutations: All workqand no play maXes jack a dull boy
6 mutations: All workqand nL play maXes jack a dull boy
&lt;span style=&quot;color:green; margin:0em&quot;&gt;u&lt;/span&gt;
Going down for swap
HotSwap example started - version 0.1
Coming up from swap!
Running state machine
7: All workqand nL play maXes jack a dull boy
8: All workqand nL play maXes jack a dull boy
9: All workqand nL play maXes jack a dull boy
&lt;span style=&quot;color:green; margin:0em&quot;&gt;q&lt;/span&gt;
Terminating with code 0&lt;/pre&gt;

&lt;/td&gt;
&lt;/tr&gt;

&lt;/tbody&gt;&lt;/table&gt;


&lt;p&gt;
&lt;br/&gt;

As you can see, version 0.2 mutates the line as it goes, while version 0.1 simply prints it forever. There are more differences than that, but you can find all that out from the code.&lt;/p&gt;


&lt;p&gt;
Enjoy! If you do end up playing with it, I’d love to hear about your experiences, or your thoughts on the design even if not.&lt;/p&gt;

&lt;br/&gt;
&lt;p&gt;
&lt;em&gt;This will probably be my last post for a while – on Saturday I leave the continent for 2 weeks and the city for 6. I will try to respond to emails and comments while I’m gone, but I may be a bit slower than usual.&lt;/em&gt;&lt;/p&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.thelowlyprogrammer.com/feeds/8001471963766584930/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.thelowlyprogrammer.com/2010/04/robustly-hot-swapping-binaries-with.html#comment-form' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1806360094658697411/posts/default/8001471963766584930'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1806360094658697411/posts/default/8001471963766584930'/><link rel='alternate' type='text/html' href='http://www.thelowlyprogrammer.com/2010/04/robustly-hot-swapping-binaries-with.html' title='Robustly hot swapping binaries (with code)'/><author><name>Eric Burnett</name><uri>http://www.blogger.com/profile/10741882872804697111</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1806360094658697411.post-579732869948510119</id><published>2010-04-23T16:37:00.010-04:00</published><updated>2021-07-17T22:47:20.774-04:00</updated><title type='text'>Indexing and enumerating subsets of a given size</title><content type='html'>&lt;p&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi8e3PZ25ipPp0ZHfRI7lyYOUl-1C5zBxmHe9dv6tmlUMJX-k2j_qBINCjhPWowB36pJ66_ojvwhrroWkpYn6MdkmW-hJdUGruTVSDTuiyn1TIYdPC29B51JvbxkFBstBqbfWrk-8y86HYS/s1600-h/9170.png&quot;&gt;&lt;img style=&quot;border-right-width: 0px; display: block; float: none; border-top-width: 0px; border-bottom-width: 0px; margin-left: auto; border-left-width: 0px; margin-right: auto&quot; title=&quot;Subsets of a 9 element set containing up to 3 elements, in Banker&amp;#39;s order&quot; border=&quot;0&quot; alt=&quot;Subsets of a 9 element set containing up to 3 elements, in Banker&amp;#39;s order&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhoLDL6KjFdU7dKmDXkzgqW9QuyccLuYNIOLhVMxclg-ncxbYbL6c_Npp5yOk1H7SDvZ4KauBXrBxuPcsMX1ReMHh2xFA5lJQUux9FoI3L3Ry9hC28g83VMkeA9TzPhWKZCnAVn6sSCVTl3/?imgmax=800&quot; width=&quot;391&quot; height=&quot;31&quot; /&gt;&lt;/a&gt;&lt;/p&gt;&lt;p&gt;I received an email yesterday from a gentleman named Calvin Miracle, asking my opinion on subset enumeration strategy. He also provided a copy of &lt;a title=&quot;Efficiently Enumerating the Subsets of a Set&quot; href=&quot;http://applied-math.org/subset.pdf&quot;&gt;this paper&lt;/a&gt;, which details an algorithm to generate a sequence of subsets in Banker’s order&lt;sup&gt;&lt;a href=&quot;#bankersOrdering&quot;&gt;1&lt;/a&gt;&lt;/sup&gt;, and asked about an algorithm to generate these subsets in a stateless manner. I’ll let him describe it:&lt;/p&gt;&lt;blockquote&gt;&lt;p&gt;Given a call to the method banker(&lt;em&gt;n&lt;/em&gt;,&lt;em&gt;k&lt;/em&gt;,&lt;em&gt;i&lt;/em&gt;), where &lt;em&gt;n&lt;/em&gt; is the size of a set, &lt;em&gt;k&lt;/em&gt; is the subset size under consideration, and &lt;em&gt;i&lt;/em&gt; is the i&#39;th subset of size &lt;em&gt;k&lt;/em&gt;, the method will return a boolean vector of size &lt;em&gt;n&lt;/em&gt;, with only &lt;em&gt;k&lt;/em&gt; TRUE values, that selects the i&#39;th&lt;em&gt; k&lt;/em&gt;-size subset from the overall set.&lt;/p&gt;&lt;/blockquote&gt;&lt;p&gt;Now, prior to this I had never heard of Banker’s sequences nor really thought about enumerating subsets, but I’m always willing to be &lt;a href=&quot;http://xkcd.com/356/&quot;&gt;nerd sniped&lt;/a&gt; so I gave it a go. Presented here is the algorithm I designed for him.&lt;/p&gt;&lt;h2&gt;Disclaimer&lt;/h2&gt;&lt;p&gt;After writing this algorithm, I did some more digging and found out that some recent research has been done into enumerating subsets of a specific size, although in lexicographic order rather than Banker’s order. &lt;a href=&quot;https://en.wikipedia.org/wiki/Combinatorial_number_system&quot;&gt;Wikipedia&lt;/a&gt; has details on this, and provides an algorithm very similar to the one I created here for except lexicographic ordering. So none of this should be seen as especially new or groundbreaking, although it was new to me and hopefully will be to you as well.&lt;/p&gt;&lt;h2&gt;Algorithm&lt;/h2&gt;&lt;p&gt;The basic idea of this algorithm is that given any choice of the first element for our subset, we can calculate how many possible ways to choose the remaining elements there are, and we know that every subset with this first element will come before every subset with a later first element according to our ordering scheme. So we can iterate through the possible choices of first element, totalling up how many subsets each represents, until we find the range that the desired index falls within. We can then recursively determine the rest of the subset in the same manner.&lt;/p&gt;&lt;p&gt;So, Let &lt;em&gt;n&lt;/em&gt; be the number of items,&lt;em&gt; k&lt;/em&gt; the size of the subsequence, and &lt;em&gt;i &lt;/em&gt;the specific index we are looking for. We will enumerate sequences by considering all subsets that start with “1”, then all that start with “01”, then all that start with “001”, etc., where “0” represents skipping an item and “1” represents selecting it. There are &lt;em&gt;n&lt;/em&gt;-1 choose &lt;em&gt;k&lt;/em&gt;-1 of the first sort, &lt;em&gt;n&lt;/em&gt;-2 choose &lt;em&gt;k&lt;/em&gt;-1 of the second, &lt;em&gt;n&lt;/em&gt;-3 choose &lt;em&gt;k&lt;/em&gt;-1 of the third, etc. So:&lt;/p&gt;&lt;table border=&quot;0&quot; cellspacing=&quot;0&quot; cellpadding=&quot;2&quot; width=&quot;440&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;       &lt;td valign=&quot;top&quot; width=&quot;54&quot;&gt;“1”:&lt;/td&gt;        &lt;td valign=&quot;top&quot; width=&quot;143&quot; align=&quot;right&quot;&gt;0&lt;/td&gt;        &lt;td valign=&quot;top&quot; width=&quot;52&quot;&gt;&amp;lt;= &lt;em&gt;i&lt;/em&gt; &amp;lt; &lt;/td&gt;        &lt;td valign=&quot;top&quot; width=&quot;189&quot;&gt;&lt;img style=&quot;border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px&quot; title=&quot;n-1Ck-1&quot; border=&quot;0&quot; alt=&quot;n-1Ck-1&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjk2dpzfKmSSBAmBSKrzFpSXEDqau9xYtyv7ewvGv1CwYxeiSXRO-XM9UlbE62eoCYmRSjNJEH54DLNPqp7Arttc9kdqV3C-ayjWoqqrYppvgDA5Xe8jrAh197ybNKCjqFN3jxvKfopdssp/?imgmax=800&quot; width=&quot;49&quot; height=&quot;32&quot; /&gt;&lt;/td&gt;     &lt;/tr&gt;
&lt;tr&gt;       &lt;td valign=&quot;top&quot; width=&quot;54&quot;&gt;“01”:&lt;/td&gt;        &lt;td valign=&quot;top&quot; width=&quot;143&quot; align=&quot;right&quot;&gt;&lt;img style=&quot;border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px&quot; title=&quot;n-1Ck-1&quot; border=&quot;0&quot; alt=&quot;n-1Ck-1&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjk2dpzfKmSSBAmBSKrzFpSXEDqau9xYtyv7ewvGv1CwYxeiSXRO-XM9UlbE62eoCYmRSjNJEH54DLNPqp7Arttc9kdqV3C-ayjWoqqrYppvgDA5Xe8jrAh197ybNKCjqFN3jxvKfopdssp/?imgmax=800&quot; width=&quot;49&quot; height=&quot;32&quot; /&gt;&lt;/td&gt;        &lt;td valign=&quot;top&quot; width=&quot;52&quot;&gt;&amp;lt;= &lt;em&gt;i&lt;/em&gt; &amp;lt;&lt;/td&gt;        &lt;td valign=&quot;top&quot; width=&quot;189&quot;&gt;&lt;img style=&quot;border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px&quot; title=&quot;n-1Ck-1 + n-2Ck-1&quot; border=&quot;0&quot; alt=&quot;n-1Ck-1 + n-2Ck-1&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhYSSCU_tHgoBnK7d7IcWTxw_20QiTJ6QnJvrxFdnmaVcxh216zVrgi5tkhyphenhyphenGMLJsyk3GauEsq1TWwW7a0HGB_4vNkWZqIRWNnhhSMSHtcpBgTESXAbAQBV5oVyJJT6B1PrOWCpnFuV6w9K/?imgmax=800&quot; width=&quot;115&quot; height=&quot;32&quot; /&gt;&lt;/td&gt;     &lt;/tr&gt;
&lt;tr&gt;       &lt;td valign=&quot;top&quot; width=&quot;54&quot;&gt;“001”:&lt;/td&gt;        &lt;td valign=&quot;top&quot; width=&quot;143&quot; align=&quot;right&quot;&gt;&lt;img style=&quot;border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px&quot; title=&quot;n-1Ck-1 + n-2Ck-1&quot; border=&quot;0&quot; alt=&quot;n-1Ck-1 + n-2Ck-1&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhYSSCU_tHgoBnK7d7IcWTxw_20QiTJ6QnJvrxFdnmaVcxh216zVrgi5tkhyphenhyphenGMLJsyk3GauEsq1TWwW7a0HGB_4vNkWZqIRWNnhhSMSHtcpBgTESXAbAQBV5oVyJJT6B1PrOWCpnFuV6w9K/?imgmax=800&quot; width=&quot;115&quot; height=&quot;32&quot; /&gt;&lt;/td&gt;        &lt;td valign=&quot;top&quot; width=&quot;52&quot;&gt;&amp;lt;= &lt;em&gt;i&lt;/em&gt; &amp;lt;&lt;/td&gt;        &lt;td valign=&quot;top&quot; width=&quot;189&quot;&gt;&lt;img style=&quot;border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px&quot; title=&quot;n-1Ck-1 + n-2Ck-1 + n-3Ck-1&quot; border=&quot;0&quot; alt=&quot;n-1Ck-1 + n-2Ck-1 + n-3Ck-1&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjPPptG0wL0Z4Ras3uwEXKa3PMYK9VGzaZRnCTPHUWDJzg97mY0IozsKeA4hEpw_RrEZ3Oa5rmfWzFGBM9tGeUNVH4S3twq3LU4GFA17-USECNdmof1GKxw8DWyZPEfo8O4srMaBRsAU3-u/?imgmax=800&quot; width=&quot;181&quot; height=&quot;32&quot; /&gt;&lt;/td&gt;     &lt;/tr&gt;
&lt;tr&gt;       &lt;td valign=&quot;top&quot; width=&quot;54&quot;&gt;…&lt;/td&gt;        &lt;td valign=&quot;top&quot; width=&quot;143&quot; align=&quot;right&quot;&gt;&amp;#160;&lt;/td&gt;        &lt;td valign=&quot;top&quot; width=&quot;52&quot;&gt;&amp;#160;&lt;/td&gt;        &lt;td valign=&quot;top&quot; width=&quot;189&quot;&gt;&amp;#160;&lt;/td&gt;     &lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;&lt;p&gt;&amp;#160;&lt;/p&gt;&lt;p&gt;Once we know what prefix &lt;em&gt;i&lt;/em&gt; has, we can recursively determine the next sequence using &lt;em&gt;n&#39;&lt;/em&gt; = &lt;em&gt;n&lt;/em&gt;-(prefix length), &lt;em&gt;k&#39; &lt;/em&gt;= &lt;em&gt;k&lt;/em&gt;-1, &lt;em&gt;i&#39; &lt;/em&gt;= &lt;em&gt;i&lt;/em&gt;-(bottom of range).&lt;/p&gt;&lt;h2&gt;Example&lt;/h2&gt;&lt;p&gt;Let &lt;em&gt;n&lt;/em&gt; = 5, &lt;em&gt;k &lt;/em&gt;= 3, &lt;em&gt;i &lt;/em&gt;= 7:&lt;/p&gt;&lt;table border=&quot;0&quot; cellspacing=&quot;0&quot; cellpadding=&quot;2&quot; width=&quot;348&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;       &lt;td valign=&quot;top&quot; width=&quot;91&quot;&gt;“1”&lt;/td&gt;        &lt;td valign=&quot;top&quot; width=&quot;15&quot; align=&quot;right&quot;&gt;0&lt;/td&gt;        &lt;td valign=&quot;top&quot; width=&quot;47&quot;&gt;&amp;lt;= &lt;em&gt;i&lt;/em&gt; &amp;lt; &lt;/td&gt;        &lt;td valign=&quot;top&quot; width=&quot;193&quot;&gt;6&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; (4C2)&lt;/td&gt;     &lt;/tr&gt;
&lt;tr&gt;       &lt;td valign=&quot;top&quot; width=&quot;91&quot;&gt;“01”:&lt;/td&gt;        &lt;td valign=&quot;top&quot; width=&quot;15&quot; align=&quot;right&quot;&gt;6&lt;/td&gt;        &lt;td valign=&quot;top&quot; width=&quot;47&quot;&gt;&amp;lt;= &lt;em&gt;i&lt;/em&gt; &amp;lt;&lt;/td&gt;        &lt;td valign=&quot;top&quot; width=&quot;193&quot;&gt;9 = 6+3&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; (4C2 + 3C2)&lt;/td&gt;     &lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;&lt;p&gt;So, initial prefix is “01”.&lt;/p&gt;&lt;p&gt;We recurse with &lt;em&gt;n&lt;/em&gt; = 3, &lt;em&gt;k&lt;/em&gt; = 2, &lt;em&gt;i&lt;/em&gt; = 1:&lt;/p&gt;&lt;table border=&quot;0&quot; cellspacing=&quot;0&quot; cellpadding=&quot;2&quot; width=&quot;348&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;       &lt;td valign=&quot;top&quot; width=&quot;91&quot;&gt;“1”&lt;/td&gt;        &lt;td valign=&quot;top&quot; width=&quot;15&quot; align=&quot;right&quot;&gt;0&lt;/td&gt;        &lt;td valign=&quot;top&quot; width=&quot;47&quot;&gt;&amp;lt;= &lt;em&gt;i&lt;/em&gt; &amp;lt; &lt;/td&gt;        &lt;td valign=&quot;top&quot; width=&quot;193&quot;&gt;2&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; (2C1)&lt;/td&gt;     &lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;&lt;p&gt;So, the next piece is “1”.&lt;/p&gt;&lt;p&gt;Finally, we recurse with &lt;em&gt;n&lt;/em&gt;=2,&lt;em&gt; k&lt;/em&gt;=1, &lt;em&gt;i&lt;/em&gt;=1:     &lt;br /&gt;
Trivially we can see that for “10” = 0, “01” = 1, so the last piece is “01”.&lt;/p&gt;&lt;p&gt;Putting this together, the sequence is “01101”,&amp;#160; so items 2, 3, and 5 of the 5 compose the 7th subset&lt;sup&gt;&lt;a href=&quot;#indexedFromZero&quot;&gt;2&lt;/a&gt;&lt;/sup&gt; of length 3.&lt;/p&gt;&lt;h2&gt;Complexity&lt;/h2&gt;&lt;p&gt;This algorithm is quite efficient – it needs to check at most &lt;em&gt;n&lt;/em&gt; prefixes in total over all the levels of recursion, since each we consider shortens our candidate set by 1. There will be at most &lt;em&gt;k &lt;/em&gt;recursive levels, and &lt;em&gt;k&lt;/em&gt; ≤ &lt;em&gt;n&lt;/em&gt;. So if we handwaive the “&lt;em&gt;a &lt;/em&gt;choose &lt;em&gt;b” &lt;/em&gt;calculations, we find that this can be done in O(&lt;em&gt;n&lt;/em&gt;) time, which is O(1) in the size of the output, and thus optimal.&lt;/p&gt;&lt;p&gt;If we &lt;em&gt;don’t&lt;/em&gt; handwaive the choice functions, we find that &lt;em&gt;n&lt;/em&gt;C&lt;em&gt;k&lt;/em&gt; is O(&lt;em&gt;n&lt;/em&gt;!) in the worst case, so the result requires O(log(&lt;em&gt;n&lt;/em&gt;!)) =O(&lt;em&gt;n&lt;/em&gt;log&lt;em&gt;n&lt;/em&gt;) bits to store. If addition is O(1) we still may need to add up to &lt;em&gt;n &lt;/em&gt;of these, so the total worst-case runtime is O(&lt;em&gt;n&lt;/em&gt;&lt;sup&gt;2&lt;/sup&gt; log&lt;em&gt;n&lt;/em&gt;), or O(&lt;em&gt;n&lt;/em&gt;log&lt;em&gt;n&lt;/em&gt;) in the size of the output. However, with small values for k we don&#39;t get anywhere close to that worst case, and indeed are closer to O(log&lt;em&gt;n&lt;/em&gt;) in size of the input. This is still quite efficient, and probably about the best that can be achieved in a function of this type. We should also consider the time needed to &lt;em&gt;calculate&lt;/em&gt; the choice values, but if we are iterating over these indices the values can be pre-cached first and then the amortized cost per subsequence lookup goes to effectively zero.&lt;/p&gt;&lt;h2&gt;Code&lt;/h2&gt;&lt;p&gt;If you want to play with this algorithm yourself, I have placed some java code implementing it &lt;a href=&quot;https://github.com/EricBurnett/EnumeratedSubsets&quot;&gt;on GitHub&lt;/a&gt;. It is as efficient as expected, and should be plenty fast enough for anything you could conceivably need it for. For example, it takes essentially no time to determine that the 160,000,000,000,000,000,000,000,000,000th subset of size 12 from a 10,000 item set is {0, 1, 2, 69, 1212, 1381, 4878, 5291, 5974, 6139, 6639, 8979}. So have at it!&lt;/p&gt;&lt;p&gt;&lt;b&gt;Update:&lt;/b&gt; Ligon Liu has kindly provided a C# port of this code, which I have added to the repository (it the c# directory).&lt;/p&gt;&lt;p&gt;&lt;b&gt;Update 2:&lt;/b&gt; Josiah Carlson has kindly provided a python port of this code, also in the repository now.&lt;/p&gt;&lt;p&gt;&lt;b&gt;Update 3:&lt;/b&gt; &lt;a href=&quot;http://richardlyon.co.uk&quot;&gt;Richard Lyon&lt;/a&gt; has kindly provided ports to both JavaScript and PHP, on GitHub as well. Thanks guys!&lt;/p&gt;&lt;p&gt;&lt;b&gt;Update 4:&lt;/b&gt; Corin Lawson has a C implementation in his &lt;a href=&quot;https://github.com/au-phiware/bankers&quot;&gt;GitHub repository&lt;/a&gt;, along with other Banker&#39;s order functions. Great!&lt;/p&gt;&lt;h2&gt;&lt;font color=&quot;#666666&quot;&gt;Footnotes&lt;/font&gt;&lt;/h2&gt;&lt;ol&gt;&lt;li&gt;&lt;a name=&quot;bankersOrdering&quot;&gt;&lt;/a&gt;The easiest way to think of Banker’s ordering is to think of comparing the &lt;em&gt;indices &lt;/em&gt;of the items that make up the subsets. So to order two subsets, take the list of indices of elements in the first subset, and compare it to that of the second subset in standard dictionary (lexicographic) order. So {2, 3, 4} comes before {2, 3, 5} but after {1, 3, 5}, for example. &lt;br /&gt;
&lt;br /&gt;
The image at the top of this post depicts the Banker’s ordering for subsets of&amp;#160; lengths 1, 2, and 3, respectively, from a set of size 9.&lt;/li&gt;
&lt;li&gt;&lt;a name=&quot;indexedFromZero&quot;&gt;&lt;/a&gt;Subsets are indexed from 0, so you may consider this the 8th subset.&lt;/li&gt;
&lt;/ol&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.thelowlyprogrammer.com/feeds/579732869948510119/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.thelowlyprogrammer.com/2010/04/indexing-and-enumerating-subsets-of.html#comment-form' title='12 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1806360094658697411/posts/default/579732869948510119'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1806360094658697411/posts/default/579732869948510119'/><link rel='alternate' type='text/html' href='http://www.thelowlyprogrammer.com/2010/04/indexing-and-enumerating-subsets-of.html' title='Indexing and enumerating subsets of a given size'/><author><name>Eric Burnett</name><uri>http://www.blogger.com/profile/10741882872804697111</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhoLDL6KjFdU7dKmDXkzgqW9QuyccLuYNIOLhVMxclg-ncxbYbL6c_Npp5yOk1H7SDvZ4KauBXrBxuPcsMX1ReMHh2xFA5lJQUux9FoI3L3Ry9hC28g83VMkeA9TzPhWKZCnAVn6sSCVTl3/s72-c?imgmax=800" height="72" width="72"/><thr:total>12</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1806360094658697411.post-6097403193265206323</id><published>2010-04-14T01:45:00.013-04:00</published><updated>2021-07-17T22:46:39.926-04:00</updated><title type='text'>Introducing: Marriage Sort</title><content type='html'>&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjKhDldNozkQank-ISULNXgW_Ge1y_vxPCznkuWeZUdQoH6OzMb_vUV2ZyOrSqZkuYSWpAFtX_VfZXAn6Vdzs3p7rTKgByJm6F5id7bM0z4gEfz5UJcPi1iyj6odf13nvdFw-1GQMDWaLYy/s1600/Marriage.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjKhDldNozkQank-ISULNXgW_Ge1y_vxPCznkuWeZUdQoH6OzMb_vUV2ZyOrSqZkuYSWpAFtX_VfZXAn6Vdzs3p7rTKgByJm6F5id7bM0z4gEfz5UJcPi1iyj6odf13nvdFw-1GQMDWaLYy/s576/Marriage.png&quot; width=&quot;576&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;
&lt;i&gt;Update: I&#39;ve created a live visualization of this algorithm, so you can see it in action - see &lt;a href=&quot;http://www.thelowlyprogrammer.com/2012/01/marriage-sort-visualization.html&quot;&gt;Marriage Sort, a Visualization.&lt;/a&gt;&lt;/i&gt;&lt;br /&gt;
&lt;br /&gt;
Two weeks ago, a link showed up on Hacker News on how to (mathematically) &lt;a href=&quot;http://www.mathpages.com/home/kmath018/kmath018.htm&quot;&gt;select the best wife&lt;/a&gt;. Tongue firmly in cheek, of course. The key takeaway from the article is that knowing only the relative rankings of items in a sequence (and assuming you can never go back to a previous one), you can maximize your chances of selecting the maximum one by letting N/e go by and then picking the next one that&#39;s better than all those. Further, to maximize your &lt;i&gt;expected value&lt;/i&gt; you only need to let the first √N - 1 go by. After rattling around in the back of my mind for a couple weeks, yesterday this tidbit coalesced into an actual Idea. Could I turn this selection routine into a sorting algorithm? &lt;br /&gt;
&lt;br /&gt;
And thus, Marriage Sort was born. I present it here purely out of interest, not because it is especially fast - if nothing else, because it is the only sorting algorithm I know of that has an average complexity of O(n&lt;sup&gt;1.5&lt;/sup&gt;)&lt;sup&gt;&lt;a href=&quot;#AlgorithmRuntimes&quot;&gt;1&lt;/a&gt;&lt;/sup&gt;. &lt;br /&gt;
&lt;br /&gt;
&lt;h2&gt;Algorithm&lt;/h2&gt;The basic idea of this algorithm is to repeatedly choose the maximum element from the first √N - 1 elements of our working set, and then scan to the end looking for elements bigger than it. When one is found, swap it to the end of the working set, and decrease the working set by one. After the pass is complete, swap out the maximum element from the first √N - 1 as well, and start again. When everything is finished (√N - 1 &amp;lt;= 0), use insertion sort to put the array into &lt;i&gt;true&lt;/i&gt; sorted order.&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg_UPzkwv1hbbjC1QKrqBr0peZBatOiOPrHeisx0G3pY1MnvqTAsghqv79-0f9wGKojmr4o8lekq-aG3gzpqfT0m6b8FGbNAfHUyLFtdw572Bqj6ESGILL-xkPX7p_37HUr3C3sdYkkKbgu/s1600/one+pass.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;145&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg_UPzkwv1hbbjC1QKrqBr0peZBatOiOPrHeisx0G3pY1MnvqTAsghqv79-0f9wGKojmr4o8lekq-aG3gzpqfT0m6b8FGbNAfHUyLFtdw572Bqj6ESGILL-xkPX7p_37HUr3C3sdYkkKbgu/s400/one+pass.png&quot; width=&quot;400&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;One pass of the Marriage Sort. Two elements are found and swapped to the end, followed by the maximum element from the first √N - 1.&lt;/div&gt;&lt;br /&gt;
&lt;br /&gt;
This works because, as we have noted from the article linked above, each element bigger than the first √N - 1 is expected to be &lt;i&gt;close&lt;/i&gt; to the largest remaining element in the array. By repeatedly taking these elements and moving them to the end, we put the array into an approximately sorted order, where elements should be reasonably close to their &#39;true&#39; positions. When this is done insertion sort will do the final rearranging, moving elements the short distances to their true positions. &lt;br /&gt;
&lt;br /&gt;
In pseudocode:&lt;br /&gt;
&lt;blockquote&gt;def marriagesort(array):&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; end = array.length&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; while true: &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;skip = sqrt(end) - 1&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;if skip &amp;lt;= 0: break&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;# Pick the best element in the first vN - 1:&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;bestPos = 0, i = 1&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;while i &amp;lt; skip:&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;if array[i] &amp;gt; array[bestPos]: bestPos = i&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;i += 1&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;# Now pull out elements &amp;gt;= array[bestPos], and move to the end:&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;i = skip&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;while i &amp;lt; end:&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;if array[i] &amp;gt;= array[bestPos]: &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;array.swap(i, end-1)&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;end -= 1&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;else: &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;i += 1&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;# Finally, move our best pivot element to the end&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;swap(bestPos, end-1)&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;end -= 1&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; # Finish off with insertion sort to put the elements into true sorted order&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; insertionsort(array)&lt;/blockquote&gt;&lt;br /&gt;
Here you can see this algorithm working on a randomized array. Many thanks to Aldo Cortesi for the wonderful &lt;a href=&quot;http://corte.si/posts/code/visualisingsorting/index.html&quot;&gt;sorting algorithm visualization framework&lt;/a&gt;! &lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjKhDldNozkQank-ISULNXgW_Ge1y_vxPCznkuWeZUdQoH6OzMb_vUV2ZyOrSqZkuYSWpAFtX_VfZXAn6Vdzs3p7rTKgByJm6F5id7bM0z4gEfz5UJcPi1iyj6odf13nvdFw-1GQMDWaLYy/s1600/Marriage.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjKhDldNozkQank-ISULNXgW_Ge1y_vxPCznkuWeZUdQoH6OzMb_vUV2ZyOrSqZkuYSWpAFtX_VfZXAn6Vdzs3p7rTKgByJm6F5id7bM0z4gEfz5UJcPi1iyj6odf13nvdFw-1GQMDWaLYy/s576/Marriage.png&quot; width=&quot;576&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;
&lt;b&gt;Update: &lt;/b&gt;Here is another visualization made using Aldo Cortesi&#39;s tools, of a 512 element array this time. If you take a close look at the insertion sort phase (the slope on the right side), you can see that it is composed out of a lot of little triangles. Each of these triangles corresponds to one pass of the marriage selection routine, and shows how far elements can be from their true sorted position.&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgKii3puz6G8MTdhKnuP_StfgDBYormdxd1vxuZbJYpWpPQVMNxSETtwExCO-1Dj2wWJlYT8BawawNeRXnH7fMMsdwYFg-9cgnRMTqAbQUU4KtxFKriXZ6T4dgmoe-4DBhnGoh85BXoNhBy/s1600/marriagesort512.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgKii3puz6G8MTdhKnuP_StfgDBYormdxd1vxuZbJYpWpPQVMNxSETtwExCO-1Dj2wWJlYT8BawawNeRXnH7fMMsdwYFg-9cgnRMTqAbQUU4KtxFKriXZ6T4dgmoe-4DBhnGoh85BXoNhBy/s576/marriagesort512.png&quot; width=&quot;576&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;
&lt;h2&gt;Complexity&lt;/h2&gt;Some quick back-of-the-napkin calculations indicate that this algorithm takes O(n&lt;sup&gt;1.5&lt;/sup&gt;) compares in the average case, although the worst case looks to be O(n&lt;sup&gt;2&lt;/sup&gt;). For the first stage (i.e. without the final insertion sort) each pass will take at most n compares and move approximately √N items to the end, requiring about √N passes. I don&#39;t know if this is a tight bound - the passes will actually speed up and require fewer compares as the working set shrinks, however I didn&#39;t want to try including that in the calculations. Similarly, after each pass all the items moved are guaranteed to be greater than all the items left, capping the distance moved items are from &quot;home&quot; to √N on average for the insertion sort. This holds the insertion sort to O(n&lt;sup&gt;1.5&lt;/sup&gt;) compares on average as well. &lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhw00OpgCUoUuwU9vVVBfGwQ6iGO5bWLBmD7n9Qf1qpXjioeIKPD4UNhRkIUXzQ9POB55L32Z8zzcAvabISe9wW3GbP2hV1iWOUPkuZP3-o-ZbuiU9326N15yoT4XI_txgARI2t9ygF0OsQ/s1600/algorithm+complexity+-+Comparisons.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhw00OpgCUoUuwU9vVVBfGwQ6iGO5bWLBmD7n9Qf1qpXjioeIKPD4UNhRkIUXzQ9POB55L32Z8zzcAvabISe9wW3GbP2hV1iWOUPkuZP3-o-ZbuiU9326N15yoT4XI_txgARI2t9ygF0OsQ/s576/algorithm+complexity+-+Comparisons.png&quot; width=&quot;576&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;
Similarly, this algorithm takes O(n&lt;sup&gt;1.5&lt;/sup&gt;) swaps in the average case (and O(n&lt;sup&gt;2&lt;/sup&gt;) in the worst case). The number of swaps could certainly be decreased if a different final sort were used - the first pass only takes Θ(n) by itself - although in practice this isn&#39;t much use since the compares will still dominate the runtime.&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg8gT4BagKLdDpyCXfp1gLQjMPEFCQFu-I3ZrHbVMRuP8jPbcq0z212YG5MH25QANXloYwdx5TgDdVyUgeUMVSL9hXjqrviWIVn7bU2Z5mMiHuuNF70kiHu-he4M5bJ5k3jQ5PfhMOJR9C4/s1600/algorithm+complexity+-+swaps.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg8gT4BagKLdDpyCXfp1gLQjMPEFCQFu-I3ZrHbVMRuP8jPbcq0z212YG5MH25QANXloYwdx5TgDdVyUgeUMVSL9hXjqrviWIVn7bU2Z5mMiHuuNF70kiHu-he4M5bJ5k3jQ5PfhMOJR9C4/s576/algorithm+complexity+-+swaps.png&quot; width=&quot;576&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;
Note that both of these graphs are log-log plots, so the important feature to look at is the slope of lines rather than the absolute position. For example, in the &quot;Swaps&quot; plot quicksort appears to be hovering around the n∙log(log(n)) line, however looking at the slope we can see that it is actually approximating n∙log(n), just with a better constant factor. &lt;br /&gt;
&lt;br /&gt;
Note 2: The x axis of these graphs is the number of elements in the array, and the y axis is the number of comparisons/sorts required. I should have labeled these better. &lt;br /&gt;
&lt;br /&gt;
&lt;h2&gt;Postscript&lt;/h2&gt;Some sample code implementing this algorithm is up &lt;a href=&quot;https://gist.github.com/365449&quot;&gt;on GitHub&lt;/a&gt; - this is the code to generate the log-log plots shown above. &lt;br /&gt;
&lt;br /&gt;
As always, questions and comments are welcome! &lt;br /&gt;
&lt;br /&gt;
&lt;h2&gt;&lt;span style=&quot;color: #666666;&quot;&gt;Footnotes&lt;/span&gt;&lt;/h2&gt;&lt;ol&gt;&lt;li&gt;&lt;a name=&quot;AlgorithmRuntimes&quot;&gt;&lt;/a&gt;Most sorting algorithms are either Θ(n&lt;sup&gt;2&lt;/sup&gt;) or Θ(n∙log(n)) in the average case. This one falls between those two extremes, making it faster (asymptotically) than algorithms like insertion sort or bubble sort but slower than quicksort or merge sort.&lt;/li&gt;
&lt;/ol&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.thelowlyprogrammer.com/feeds/6097403193265206323/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.thelowlyprogrammer.com/2010/04/introducing-marriage-sort.html#comment-form' title='22 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1806360094658697411/posts/default/6097403193265206323'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1806360094658697411/posts/default/6097403193265206323'/><link rel='alternate' type='text/html' href='http://www.thelowlyprogrammer.com/2010/04/introducing-marriage-sort.html' title='Introducing: Marriage Sort'/><author><name>Eric Burnett</name><uri>http://www.blogger.com/profile/10741882872804697111</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjKhDldNozkQank-ISULNXgW_Ge1y_vxPCznkuWeZUdQoH6OzMb_vUV2ZyOrSqZkuYSWpAFtX_VfZXAn6Vdzs3p7rTKgByJm6F5id7bM0z4gEfz5UJcPi1iyj6odf13nvdFw-1GQMDWaLYy/s72-c/Marriage.png" height="72" width="72"/><thr:total>22</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1806360094658697411.post-3705254461079453753</id><published>2010-03-17T00:38:00.005-04:00</published><updated>2021-07-17T22:45:27.995-04:00</updated><title type='text'>Visualizing RGB, take two - Update</title><content type='html'>&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiWwJyg7oMhiEIBWjLIuizjPF_byouDhBLTalB8mQ0Uc-QNIFptyBcqiiaIsQoL-5yuYjGQWNFMcja9SkAyq3BmcFbH_Sp6CrabHWgHmiC1Ujwp98ygoyTdjvXE-rByagBKnIWmLnDimudA/s1600-h/MonaLisa_allRGBv2_cap40_downsampled.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiWwJyg7oMhiEIBWjLIuizjPF_byouDhBLTalB8mQ0Uc-QNIFptyBcqiiaIsQoL-5yuYjGQWNFMcja9SkAyq3BmcFbH_Sp6CrabHWgHmiC1Ujwp98ygoyTdjvXE-rByagBKnIWmLnDimudA/s320/MonaLisa_allRGBv2_cap40_downsampled.png&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;
When I posted my &lt;a href=&quot;http://www.thelowlyprogrammer.com/2010/02/visualizing-rgb-take-two.html&quot;&gt;&#39;Visualizing RGB, take two&#39;&lt;/a&gt; article last month, an anonymous commenter going by the name &lt;i&gt;Full-size&lt;/i&gt; had a good suggestion - I should use a form of &lt;a href=&quot;https://en.wikipedia.org/wiki/Error_diffusion&quot;&gt;error diffusion&lt;/a&gt; to better hide the pixel errors when selecting the colours to use. Within a couple days I had this implemented, and wow does it make an improvement! Unfortunately, between school and needing to reinstall Windows I never ended up posting the results. So, three weeks late, here they are.&lt;br /&gt;
&lt;br /&gt;
&lt;h2&gt;Results for Barcelona image&lt;/h2&gt;As you may recall, the goal of the algorithm was to transform a source image into a 4096x4096 version that uses every RGB colour exactly once. As a reminder, here is the original image, and what my algorithm previously did with it:&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjoadbu-wXRqQfMMdiADlbKS1U24qESKnz9qBreSg3xRLMZhX8-BAwUqrfIyFpHSAvBg0kYq8GtrdJNfCA4H7_XUtqlwMxIYq5-t_2nMsBErCvh2l7xcgnwSpQJe_hTk_o4nR7e3xLsKGA4/s1600-h/2.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;200&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjoadbu-wXRqQfMMdiADlbKS1U24qESKnz9qBreSg3xRLMZhX8-BAwUqrfIyFpHSAvBg0kYq8GtrdJNfCA4H7_XUtqlwMxIYq5-t_2nMsBErCvh2l7xcgnwSpQJe_hTk_o4nR7e3xLsKGA4/s200/2.png&quot; width=&quot;200&quot; /&gt;&lt;/a&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjv0v_Fpb9sKglVAH0F3OqtRYDRHeRdNN02yrcmIjZvKwCX6DKQ7LCewJi_oFSbEPF3Et59rjfbe7c2NNfwxxzEvnze2V4Z5Avbd1WpjdA6jDBwijuEdYnmsV3aF3PAXH_tl7ulxdUQg0uP/s1600-h/2_allRGBv2_big_downsampled.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;200&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjv0v_Fpb9sKglVAH0F3OqtRYDRHeRdNN02yrcmIjZvKwCX6DKQ7LCewJi_oFSbEPF3Et59rjfbe7c2NNfwxxzEvnze2V4Z5Avbd1WpjdA6jDBwijuEdYnmsV3aF3PAXH_tl7ulxdUQg0uP/s200/2_allRGBv2_big_downsampled.png&quot; width=&quot;200&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div width=&quot;400px&quot;&gt;&lt;div style=&quot;float: left; text-align: center; width: 200px;&quot;&gt;Original&lt;/div&gt;&lt;div style=&quot;float: right; text-align: center; width: 200px;&quot;&gt;Result from previous version&lt;/div&gt;&lt;/div&gt;&lt;br /&gt;
&lt;br /&gt;
Now, take a look at how it looks using a form of error diffusion (with error terms capped at 60):&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjiqsGRTbPkvC344rjb7W6vO-Q66eqZN8NSTJVCytCODalPlkOjgprBLoB2uwEfyoEj8yirkdNWf7ISg6r2MIYQH-ozW6ZKMDbe29M-sRM93uMZSmPadjehAB8m_cRwIp3X5BvKoD_Y_K_U/s1600-h/3_allRGBv2_cap60_downsampled.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;320&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjiqsGRTbPkvC344rjb7W6vO-Q66eqZN8NSTJVCytCODalPlkOjgprBLoB2uwEfyoEj8yirkdNWf7ISg6r2MIYQH-ozW6ZKMDbe29M-sRM93uMZSmPadjehAB8m_cRwIp3X5BvKoD_Y_K_U/s320/3_allRGBv2_cap60_downsampled.png&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;
(Full 50 meg png &lt;a href=&quot;http://www.mediafire.com/file/2gmfkxhzmyo/3_allRGBv2_cap60_big.zip&quot;&gt;available here&lt;/a&gt;). Much nicer to look at! Take a look at the sky in particular - where it used to go psychedelic trying to deal with all that near white, large portions now end up simply turning into a uniform gray. The new version is worse in a couple spots (e.g. the wheel wells of the car), but overall I think it is hugely improved. Now, I wonder how the new version would do on a harder image?&lt;br /&gt;
&lt;br /&gt;
&lt;h2&gt;Results for the Mona Lisa&lt;/h2&gt;As promised, I am also posting the results of running this algorithm on an image of the Mona Lisa. This is an especially difficult image, because the colour palette is very limited - notice the lack of blue in particular. First, let&#39;s take a look at the original, and the result from the previous version of the code:&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjozHhLM_0vAXKKIlU2CqXmn7UST04_jLLs-zNoD6omS2crcVVFIptO3tBsHcohpiOuUolnaZQDrh0Uy587H7WpLYmgyKGrP8eY82aKJfrXfJoygkG5NV44ESWlqhTqCAwJ2z8WIZANVx3N/s1600-h/MonaLisa_downsampled.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;200&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjozHhLM_0vAXKKIlU2CqXmn7UST04_jLLs-zNoD6omS2crcVVFIptO3tBsHcohpiOuUolnaZQDrh0Uy587H7WpLYmgyKGrP8eY82aKJfrXfJoygkG5NV44ESWlqhTqCAwJ2z8WIZANVx3N/s200/MonaLisa_downsampled.png&quot; width=&quot;200&quot; /&gt;&lt;/a&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhkGEJA8OFoGqP8J2hf7FVuA7np-z1gAQVSYt2qV7opwMhKnfmd9tl5i2vMjT2cRzPvBEHxzjhF0ce6zGO9gS0BSoxC9T_BlNISQozvTT03yVE0R9zuEVlGBek30GeyHb_Rvz3QlxWBHVdl/s1600-h/MonaLisa_allRGBv2_cap0.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;200&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhkGEJA8OFoGqP8J2hf7FVuA7np-z1gAQVSYt2qV7opwMhKnfmd9tl5i2vMjT2cRzPvBEHxzjhF0ce6zGO9gS0BSoxC9T_BlNISQozvTT03yVE0R9zuEVlGBek30GeyHb_Rvz3QlxWBHVdl/s200/MonaLisa_allRGBv2_cap0.png&quot; width=&quot;200&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div width=&quot;400px&quot;&gt;&lt;div style=&quot;float: left; text-align: center; width: 200px;&quot;&gt;Original&lt;/div&gt;&lt;div style=&quot;float: right; text-align: center; width: 200px;&quot;&gt;Result from previous version&lt;/div&gt;&lt;/div&gt;&lt;br /&gt;
&lt;br /&gt;
Ouch. Poor Lisa. Still, let&#39;s forge on and see how the new version does, shall we?&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiWwJyg7oMhiEIBWjLIuizjPF_byouDhBLTalB8mQ0Uc-QNIFptyBcqiiaIsQoL-5yuYjGQWNFMcja9SkAyq3BmcFbH_Sp6CrabHWgHmiC1Ujwp98ygoyTdjvXE-rByagBKnIWmLnDimudA/s1600-h/MonaLisa_allRGBv2_cap40_downsampled.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiWwJyg7oMhiEIBWjLIuizjPF_byouDhBLTalB8mQ0Uc-QNIFptyBcqiiaIsQoL-5yuYjGQWNFMcja9SkAyq3BmcFbH_Sp6CrabHWgHmiC1Ujwp98ygoyTdjvXE-rByagBKnIWmLnDimudA/s320/MonaLisa_allRGBv2_cap40_downsampled.png&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;
(Full 50 meg png &lt;a href=&quot;http://www.mediafire.com/file/cd4mzzniuzl/MonaLisa_allRGBv2_cap40_big.zip&quot;&gt;available here&lt;/a&gt;). Considerably better overall, although the colour burning on the forehead and neck is pretty ugly to look at. Still, considering we are trying to use every RGB colour once, I think the results are quite decent.&lt;br /&gt;
&lt;br /&gt;
&lt;h2&gt;Postscript&lt;/h2&gt;The code to achieve this is in the same place as before, updated &lt;a href=&quot;https://github.com/EricBurnett/AllRGBv2&quot;&gt;on GitHub&lt;/a&gt;. Right now you have to change the source code to edit the error diffusion cap, sorry - but feel free to fix that!</content><link rel='replies' type='application/atom+xml' href='http://www.thelowlyprogrammer.com/feeds/3705254461079453753/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.thelowlyprogrammer.com/2010/03/visualizing-rgb-take-two-update.html#comment-form' title='8 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1806360094658697411/posts/default/3705254461079453753'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1806360094658697411/posts/default/3705254461079453753'/><link rel='alternate' type='text/html' href='http://www.thelowlyprogrammer.com/2010/03/visualizing-rgb-take-two-update.html' title='Visualizing RGB, take two - Update'/><author><name>Eric Burnett</name><uri>http://www.blogger.com/profile/10741882872804697111</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiWwJyg7oMhiEIBWjLIuizjPF_byouDhBLTalB8mQ0Uc-QNIFptyBcqiiaIsQoL-5yuYjGQWNFMcja9SkAyq3BmcFbH_Sp6CrabHWgHmiC1Ujwp98ygoyTdjvXE-rByagBKnIWmLnDimudA/s72-c/MonaLisa_allRGBv2_cap40_downsampled.png" height="72" width="72"/><thr:total>8</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1806360094658697411.post-3027817597499477553</id><published>2010-03-02T22:57:00.008-05:00</published><updated>2021-07-17T22:43:59.578-04:00</updated><title type='text'>Writing an efficient Sieve of Eratosthenes</title><content type='html'>&lt;script language=&#39;JavaScript&#39; type=&#39;text/javascript&#39;&gt;
function expandContract(id) {
  var e = document.getElementById(id);
  if (e.style.display == &quot;block&quot;) {
    e.style.display = &quot;none&quot;;
  } else {
    e.style.display = &quot;block&quot;;
  }
}
&lt;/script&gt;&lt;p&gt;&lt;i&gt;See also the &lt;a href=&quot;http://www.thelowlyprogrammer.com/2012/08/primes-part-2-segmented-sieve.html&quot;&gt;followup post&lt;/a&gt; containing a segmented sieve implementation in Go that beats all these.&lt;/i&gt;&lt;/p&gt;&lt;br /&gt;
&lt;p&gt;I recently came across &lt;a href=&quot;http://www.cs.hmc.edu/~oneill/papers/Sieve-JFP.pdf&quot;&gt;a paper&lt;/a&gt; by Melissa E. O&#39;Neill on implementing a lazily evaluated &lt;a href=&quot;https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes&quot;&gt;Sieve of Eratosthenes&lt;/a&gt;. It was a very interesting read, although for a non-Haskeller understanding the code was certainly tricky. And by happy happenstance, not two weeks later I ended up needing a prime generator of my own for one of the &lt;a href=&quot;http://projecteuler.net/&quot;&gt;Project Euler&lt;/a&gt; problems. This, I thought, was the perfect opportunity to try implementing the algorithm from that paper! That thought ended up leading me on a many-day journey to see how efficient an implementation I could make. This post details the key changes my code went through in that time, culminating in a C# algorithm that can sieve around 20 million candidates per second (or a Python version that can do 1.4 million). &lt;/p&gt;&lt;h2&gt;Disclaimers&lt;/h2&gt;&lt;p&gt;&lt;ol&gt;&lt;li&gt;During the course of this journey I learned about segmented sieves and the &lt;a href=&quot;https://en.wikipedia.org/wiki/Sieve_of_Atkin&quot;&gt;Sieve of Atkin&lt;/a&gt;, but I decided not to make use of either of these, instead sticking to the algorithm I chose from the start. Maybe next time.&lt;/li&gt;
&lt;li&gt;If you are simply looking for the fastest implementation possible, I point you towards &lt;a href=&quot;http://cr.yp.to/primegen.html&quot;&gt;http://cr.yp.to/primegen.html&lt;/a&gt;, which is written in highly optimized C and at least an order of magnitude faster than mine.&lt;/li&gt;
&lt;/ol&gt;&lt;/p&gt;&lt;h2&gt;Version 1: Straight re-implementation of algorithm from paper&lt;/h2&gt;&lt;u&gt;Language:&lt;/u&gt; Python&lt;u&gt;&lt;br /&gt;
Time complexity:&lt;/u&gt;&lt;b&gt; &lt;/b&gt;O(n&amp;#8729;log(n)&amp;#8729;log(log(n)))&lt;br /&gt;
&lt;u&gt;Space complexity&lt;/u&gt;&lt;b&gt;: &lt;/b&gt;O(&amp;#8730;n/log(n))&lt;br /&gt;
&lt;u&gt;Candidates in 1 sec:&lt;/u&gt; ~300,000 &lt;br /&gt;
&lt;br /&gt;
&lt;a href=&quot;https://gist.github.com/320271#file_v1.py&quot;&gt;Code&lt;/a&gt; &lt;a href=&quot;javascript:expandContract(&#39;v1.py&#39;)&quot;&gt;(expand/contract)&lt;/a&gt;&lt;br /&gt;
&lt;div id=&quot;v1.py&quot; class=&quot;expandable&quot; style=&quot;display:none;&quot;&gt;&lt;script src=&quot;https://gist.github.com/320271.js?file=v1.py&quot;&gt;&lt;/script&gt;&lt;/div&gt;&lt;br /&gt;
&lt;p&gt;This is simply a re-implementation in Python of the Haskell code from the paper above, although it loses some of the elegance in the translation.  You&#39;ll note that I am measuring performance with the very unscientific &#39;candidates in 1 sec&#39; - since I am trying to compare algorithms with different asymptotic complexities in different languages with order-of-magnitude performance differences, it seemed like the easiest way to get a feel for the general magnitudes in this apples-to-oranges comparison. &lt;/p&gt;&lt;br /&gt;
&lt;p&gt;The key idea here is that for each prime we encounter, we make a generator for multiples of it (starting at p&lt;sup&gt;2&lt;/sup&gt;) that we can use to filter out our candidates. For efficiency, all multiples of 2 and 3 are skipped automatically as well, since that reduces the number of candidates to check by two thirds. These generators are all added to a min-heap, keyed by the next value each will generate. Then we can keep checking the next candidate and seeing if it matches the top of the heap. If it does, pop that, increment it, and push it back on keyed by the next value. If not, the candidate must be prime, so build a new generator and add that instead. &lt;/p&gt;&lt;br /&gt;
&lt;p&gt;So, let&#39;s try an example of this. The first candidate we try is 5 (remembering that all multiples of 2 and 3 are already gone), and our heap is empty. This becomes the first item on the heap, and then 7 is checked: &lt;blockquote&gt;5? []&lt;br /&gt;
&lt;div class=&quot;indented&quot;&gt;Nope, prime&lt;/div&gt;7? [25: 25,35,55,...]&lt;br /&gt;
&lt;div class=&quot;indented&quot;&gt;Nope, prime&lt;/div&gt;11? [25:25,35,55,... 49:49,77,91,...]&lt;br /&gt;
&lt;div class=&quot;indented&quot;&gt;Nope, prime&lt;/div&gt;13? [25:25,35,55,... 49:49,77,91,... 121:121,143,187...]&lt;br /&gt;
&lt;div class=&quot;indented&quot;&gt;Nope, prime&lt;/div&gt;17? [25:25,35,55,... 49:49,77,91,... 121:121,143,187... 169:169,221,247,...] &lt;br /&gt;
&lt;div class=&quot;indented&quot;&gt;Nope, prime&lt;/div&gt;19? [25:25,35,55,... 49:49,77,91,... 121:121,143,187... 169:169,221,247,...  ...] &lt;br /&gt;
&lt;div class=&quot;indented&quot;&gt;Nope, prime&lt;/div&gt;23? [25:25,35,55,... 49:49,77,91,... 121:121,143,187... 169:169,221,247,...  ...] &lt;br /&gt;
&lt;div class=&quot;indented&quot;&gt;Nope, prime&lt;/div&gt;25? [25:25,35,55,... 49:49,77,91,... 121:121,143,187... 169:169,221,247,...  ...]&lt;br /&gt;
&lt;div class=&quot;indented&quot;&gt;Aha! Matches the top of our heap...not prime&lt;/div&gt;29? [35:35,55,65,... 49:49,77,91,... 121:121,143,187... 169:169,221,247,...  ...]&lt;br /&gt;
&lt;div class=&quot;indented&quot;&gt;Nope, prime&lt;/div&gt;...&lt;/blockquote&gt;&lt;/p&gt;&lt;p&gt;Make sense? This is the algorithm straight from the paper, so if my explanation is lacking you could always try there. When I write it down this way, however, an optimization springs out at me - why are we using a heap at all? At any given time we are querying for a single value, and don&#39;t actually care about the other items. Instead of updating a heap structure all the time, we can simply use a hash table instead. Strictly speaking a multi-way hash table, since some values will have multiple prime factors. &lt;/p&gt;&lt;h2&gt;Version 2: Using a hash table&lt;/h2&gt;&lt;u&gt;Language:&lt;/u&gt; Python&lt;u&gt;&lt;br /&gt;
Time complexity:&lt;/u&gt;&lt;b&gt; &lt;/b&gt;O(n&amp;#8729;log(log(n)))&lt;br /&gt;
&lt;u&gt;Space complexity&lt;/u&gt;&lt;b&gt;: &lt;/b&gt;O(&amp;#8730;n/log(n))&lt;br /&gt;
&lt;u&gt;Candidates in 1 sec:&lt;/u&gt; ~1,400,000 &lt;br /&gt;
&lt;br /&gt;
&lt;a href=&quot;https://gist.github.com/320271#file_v2.py&quot;&gt;Code&lt;/a&gt; &lt;a href=&quot;javascript:expandContract(&#39;v2.py&#39;)&quot;&gt;(expand/contract)&lt;/a&gt;&lt;br /&gt;
&lt;div id=&quot;v2.py&quot; class=&quot;expandable&quot; style=&quot;display:none;&quot;&gt;&lt;script src=&quot;https://gist.github.com/320271.js?file=v2.py&quot;&gt;&lt;/script&gt;&lt;/div&gt;&lt;br /&gt;
&lt;p&gt;Not bad, under 50 lines of code and with good complexity. This is as far as I know how to go with Python, and it is a dynamic language anyways which isn&#39;t really suited to this kind of number crunching, so for the next version I&#39;m switching to C#. There I know the time required for most operations, and more importantly, I have a good profiler I know how to use. &lt;/p&gt;&lt;h2&gt;Version 3: Re-implement in C#&lt;/h2&gt;&lt;u&gt;Language:&lt;/u&gt; C#&lt;u&gt;&lt;br /&gt;
Time complexity:&lt;/u&gt;&lt;b&gt; &lt;/b&gt;O(n&amp;#8729;log(log(n)))&lt;br /&gt;
&lt;u&gt;Space complexity&lt;/u&gt;&lt;b&gt;: &lt;/b&gt;O(&amp;#8730;n/log(n))&lt;br /&gt;
&lt;u&gt;Candidates in 1 sec:&lt;/u&gt; ~10,000,000 &lt;br /&gt;
&lt;br /&gt;
&lt;a href=&quot;https://gist.github.com/320271#file_v3.cs&quot;&gt;Code&lt;/a&gt; &lt;a href=&quot;javascript:expandContract(&#39;v3.cs&#39;)&quot;&gt;(expand/contract)&lt;/a&gt;&lt;br /&gt;
&lt;div id=&quot;v3.cs&quot; class=&quot;expandable&quot; style=&quot;display:none;&quot;&gt;&lt;script src=&quot;https://gist.github.com/320271.js?file=v3.cs&quot;&gt;&lt;/script&gt;&lt;/div&gt;&lt;br /&gt;
&lt;p&gt;Wow, significant speedup there! And with a modest 25 line code hit (plus a main method for convenience), that is certainly reasonable. A little profiling tells me that the bottleneck is the list operations, which isn&#39;t too much of a surprise. Of course, C#&#39;s native list implementation isn&#39;t necessarily going to be the fastest for our exact situation - as is often the case when optimizing, the next step is to write our own data structure. Here I use a dead simple data structure, essentially just an array with an Add operation that dynamically resizes it. &lt;/p&gt;&lt;h2&gt;Version 4: Use a custom structure for the &#39;multi&#39; in multi dictionary&lt;/h2&gt;&lt;u&gt;Language:&lt;/u&gt; C#&lt;u&gt;&lt;br /&gt;
Time complexity:&lt;/u&gt;&lt;b&gt; &lt;/b&gt;O(n&amp;#8729;log(log(n)))&lt;br /&gt;
&lt;u&gt;Space complexity&lt;/u&gt;&lt;b&gt;: &lt;/b&gt;O(&amp;#8730;n/log(n))&lt;br /&gt;
&lt;u&gt;Candidates in 1 sec:&lt;/u&gt; ~12,000,000 &lt;br /&gt;
&lt;br /&gt;
&lt;a href=&quot;https://gist.github.com/320271#file_v4.cs&quot;&gt;Code&lt;/a&gt; &lt;a href=&quot;javascript:expandContract(&#39;v4.cs&#39;)&quot;&gt;(expand/contract)&lt;/a&gt;&lt;br /&gt;
&lt;div id=&quot;v4.cs&quot; class=&quot;expandable&quot; style=&quot;display:none;&quot;&gt;&lt;script src=&quot;https://gist.github.com/320271.js?file=v4.cs&quot;&gt;&lt;/script&gt;&lt;/div&gt;&lt;br /&gt;
&lt;p&gt;Not bad, a modest speedup in this version. But I must admit, I cheated a little. The main benefit of this new data structure is not the speedup gained at this step. No, the real speedup is allowing an essentially free Clear operation. Because as useful as a hash table is, we can re-write it too to get even more of a speedup. The idea here is that most of our current iterators will have a &#39;next&#39; value that differ by at most &amp;#8730;n or so, so a circular array will be better for cache locality, remove the cost of hashing, and let us re-use the &#39;multi&#39; objects. Note that a circular array is essentially a giant array, except only a small sliding window is actually backed by memory and allowed to contain values at any given time. &lt;/p&gt;&lt;h2&gt;Version 5: Use a multi cyclic array instead of hash table&lt;/h2&gt;&lt;u&gt;Language:&lt;/u&gt; C#&lt;u&gt;&lt;br /&gt;
Time complexity:&lt;/u&gt;&lt;b&gt; &lt;/b&gt;O(n&amp;#8729;log(log(n)))&lt;br /&gt;
&lt;u&gt;Space complexity&lt;/u&gt;&lt;b&gt;: &lt;/b&gt;O(&amp;#8730;n)&lt;br /&gt;
&lt;u&gt;Candidates in 1 sec:&lt;/u&gt; ~20,000,000 &lt;br /&gt;
&lt;br /&gt;
&lt;a href=&quot;https://gist.github.com/320271#file_v5.cs&quot;&gt;Code&lt;/a&gt; &lt;a href=&quot;javascript:expandContract(&#39;v5.cs&#39;)&quot;&gt;(expand/contract)&lt;/a&gt;&lt;br /&gt;
&lt;div id=&quot;v5.cs&quot; class=&quot;expandable&quot; style=&quot;display:none;&quot;&gt;&lt;script src=&quot;https://gist.github.com/320271.js?file=v5.cs&quot;&gt;&lt;/script&gt;&lt;/div&gt;&lt;br /&gt;
&lt;p&gt;Ok, that is about as far as I&#39;m willing to go. Beyond this point I expect I&#39;ll start getting into low-benefit, high-complexity optimizations, and I don&#39;t want to go down that road. Although by some counts I am already there - from version 3 there has been a 2x speedup, but at the cost of doubling the code size and ending up with algorithms that fall firmly within the realm of &#39;tricky&#39;. If I actually had to maintain a prime sieve in a professional setting that wasn&#39;t absolutely time critical, I think I would be going with version 3 - the later code starts to impose too much of a mental tax. &lt;/p&gt;&lt;br /&gt;
&lt;p&gt;And there you have it, a reasonably efficient Sieve of Eratosthenes, and for the most part without &#39;tricky&#39; optimizations. Let me know what you think! &lt;/p&gt;&lt;h2&gt;Postscript&lt;/h2&gt;&lt;p&gt;In the comments, Isaac was wondering how these compared to &quot;vanilla array&quot; implementations (essentially, pre-allocate the array and cross-off), so I have added two array implementations to GitHub (&lt;a href=https://gist.github.com/320271#file_array1.py&gt;Python&lt;/a&gt;, &lt;a href=https://gist.github.com/320271#file_array2.cs&gt;C#&lt;/a&gt;) for comparision. Both are about 4 times faster than the comparative best in their language, testing 5.5 million and 75 million candidates in one second (respectively). The Python version runs out of memory somewhere before 500 million candidates, and the C# version can&#39;t get beyond about 2 billion due to limitations of the BitArray class.&lt;/p&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.thelowlyprogrammer.com/feeds/3027817597499477553/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.thelowlyprogrammer.com/2010/03/writing-efficient-seive-of-eratosthenes.html#comment-form' title='10 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1806360094658697411/posts/default/3027817597499477553'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1806360094658697411/posts/default/3027817597499477553'/><link rel='alternate' type='text/html' href='http://www.thelowlyprogrammer.com/2010/03/writing-efficient-seive-of-eratosthenes.html' title='Writing an efficient Sieve of Eratosthenes'/><author><name>Eric Burnett</name><uri>http://www.blogger.com/profile/10741882872804697111</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>10</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1806360094658697411.post-8407591851905561974</id><published>2010-02-20T01:40:00.006-05:00</published><updated>2021-07-17T22:42:35.313-04:00</updated><title type='text'>Visualizing RGB, take two</title><content type='html'>&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;http://allrgb.com/barcelona&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjv0v_Fpb9sKglVAH0F3OqtRYDRHeRdNN02yrcmIjZvKwCX6DKQ7LCewJi_oFSbEPF3Et59rjfbe7c2NNfwxxzEvnze2V4Z5Avbd1WpjdA6jDBwijuEdYnmsV3aF3PAXH_tl7ulxdUQg0uP/s320/2_allRGBv2_big_downsampled.png&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;
&lt;b&gt;Update:&lt;/b&gt; The follow-up post can be found &lt;a href=http://www.thelowlyprogrammer.com/2010/03/visualizing-rgb-take-two-update.html&gt;here&lt;/a&gt;.&lt;br /&gt;
&lt;br /&gt;
&lt;p&gt;About 3 weeks ago, I &lt;a href=&quot;http://www.thelowlyprogrammer.com/2010/01/visualizing-rgb.html&quot;&gt;wrote about&lt;/a&gt; a program I created to transform pictures into all-RGB images (4096x4096 images using each possible RGB colour exactly once). It worked by ordering pixels based on a Hilbert ordering of the 3d colour cube and then re-colouring them in order, and while it produced interesting images, it pretty much failed at the stated goal of “keep[ing] the overall look untouched”. The problem was that the general hue of the pixels was often very shifted from the input, so the overall features were preserved but the colour balance was not. So for the past week or so I’ve been working on a new program, one that will (hopefully!) do a better job of keeping the base look intact. As with last time, I’m using an image I took in Barcelona for testing – let me know if you have a different one you’d like to see.&lt;/p&gt;&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjoadbu-wXRqQfMMdiADlbKS1U24qESKnz9qBreSg3xRLMZhX8-BAwUqrfIyFpHSAvBg0kYq8GtrdJNfCA4H7_XUtqlwMxIYq5-t_2nMsBErCvh2l7xcgnwSpQJe_hTk_o4nR7e3xLsKGA4/s1600-h/2.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;200&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjoadbu-wXRqQfMMdiADlbKS1U24qESKnz9qBreSg3xRLMZhX8-BAwUqrfIyFpHSAvBg0kYq8GtrdJNfCA4H7_XUtqlwMxIYq5-t_2nMsBErCvh2l7xcgnwSpQJe_hTk_o4nR7e3xLsKGA4/s200/2.png&quot; width=&quot;200&quot; /&gt;&lt;/a&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgh-h4lod0I53U8-LDs58TO0KFl1_JfMrhg6Brv-QFl4WKYSUKpvY53G6kyxX-loDcU3z_risMJz0Tlc7vKYnw-cwm8BG9eJLTwExv7ItD6wrx9EmbpVaKRSLUwN7wPNFGmn1dBh7o20rNc/s1600-h/2_allrgbify_small.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-bottom: 1em; margin-left: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;200&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgh-h4lod0I53U8-LDs58TO0KFl1_JfMrhg6Brv-QFl4WKYSUKpvY53G6kyxX-loDcU3z_risMJz0Tlc7vKYnw-cwm8BG9eJLTwExv7ItD6wrx9EmbpVaKRSLUwN7wPNFGmn1dBh7o20rNc/s200/2_allrgbify_small.png&quot; width=&quot;200&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div width=&quot;400px&quot;&gt;&lt;div style=&quot;float: left; text-align: center; width: 200px;&quot;&gt;Original&lt;/div&gt;&lt;div style=&quot;float: right; text-align: center; width: 200px;&quot;&gt;Result From Take One&lt;/div&gt;&lt;/div&gt;&lt;br /&gt;
&lt;h2&gt;Choose the closest colour…&lt;/h2&gt;&lt;p&gt;My idea this time was that instead of choosing an ordering of the pixels, it would be better to try to minimize the distance between the source and destination colours overall. The easiest way I could think of was to simply choose pixels at random, and assign them the “closest” colour remaining. Hopefully deviations would occur in all directions equally, so the average colour of a region would be as close as possible to the source. By popular demand, I will try to make this algorithm a little more explicit this time: &lt;br /&gt;
&lt;ol&gt;&lt;li&gt;Go through the pixels in the source image in a random order. &lt;br /&gt;
&lt;ol type=&quot;a&quot;&gt;&lt;li&gt;For each, select the closest remaining unused colour, by Euclidean distance between the coordinates in the colour space.&lt;/li&gt;
&lt;li&gt;Assign the found colour as the output colour of the pixel, and mark it used.&lt;/li&gt;
&lt;/ol&gt;&lt;/li&gt;
&lt;/ol&gt;&lt;/p&gt;&lt;h2&gt;But in which colour space?&lt;/h2&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgL05rf9tu2ONRx9Id-iUk86HDhZQJCy-d_lnHVU0yJXEi_9Rm9esLxm90qVVv7vLh0QMdQn3P_7zRLVA64s8efjyi8jrjTxRnMBkZg_4vpKEOkwcbJ_LCdD7L8St5tKYVJUrWA2jdG-m_l/s1600-h/colour+spaces.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;150&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgL05rf9tu2ONRx9Id-iUk86HDhZQJCy-d_lnHVU0yJXEi_9Rm9esLxm90qVVv7vLh0QMdQn3P_7zRLVA64s8efjyi8jrjTxRnMBkZg_4vpKEOkwcbJ_LCdD7L8St5tKYVJUrWA2jdG-m_l/s400/colour+spaces.png&quot; width=&quot;400&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;Sources: &lt;a href=&quot;https://en.wikipedia.org/wiki/File:RGB_farbwuerfel.jpg&quot;&gt;one&lt;/a&gt; and &lt;a href=&quot;https://en.wikipedia.org/wiki/File:Hsl-hsv_models.svg&quot;&gt;two&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;
&lt;p&gt;A key question I had was which colour space would be best for preserving hues? There are a number of different “colour solids” that I could use coordinates from, with RGB being only one of many. I had a strong suspicion that something like HSL would do better than using RGB directly, but the easiest way to find out which to do a direct comparison. I tried the RGB cube as well as HSL and HSV cylinders for the comparison. My test images are presented below. &lt;/p&gt;&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjoadbu-wXRqQfMMdiADlbKS1U24qESKnz9qBreSg3xRLMZhX8-BAwUqrfIyFpHSAvBg0kYq8GtrdJNfCA4H7_XUtqlwMxIYq5-t_2nMsBErCvh2l7xcgnwSpQJe_hTk_o4nR7e3xLsKGA4/s1600-h/2.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;200&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjoadbu-wXRqQfMMdiADlbKS1U24qESKnz9qBreSg3xRLMZhX8-BAwUqrfIyFpHSAvBg0kYq8GtrdJNfCA4H7_XUtqlwMxIYq5-t_2nMsBErCvh2l7xcgnwSpQJe_hTk_o4nR7e3xLsKGA4/s200/2.png&quot; width=&quot;200&quot; /&gt;&lt;/a&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgkCCsVyv0Qj3AfOrq3RxV-t90SA5wtSzUkDC9P5M_6971ZKKwNmD417xb3MSQi-TAXKstkQEZYZrb4GenH8wdyBxAQMo7NtMOkVbu7LWA5tGQCOnttazIRvPI1F_-HE1WjlxNm2gbUiTNT/s1600-h/2_allRGBv2_RGB.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;200&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgkCCsVyv0Qj3AfOrq3RxV-t90SA5wtSzUkDC9P5M_6971ZKKwNmD417xb3MSQi-TAXKstkQEZYZrb4GenH8wdyBxAQMo7NtMOkVbu7LWA5tGQCOnttazIRvPI1F_-HE1WjlxNm2gbUiTNT/s200/2_allRGBv2_RGB.png&quot; width=&quot;200&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div&gt;&lt;div style=&quot;float: left; text-align: center; width: 200px;&quot;&gt;Original&lt;/div&gt;&lt;div style=&quot;float: right; text-align: center; width: 200px;&quot;&gt;RGB&lt;/div&gt;&lt;/div&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhDHqag23pojoFAG6WzmNbCDI9FC2I-rK-VdfyAHB0TAuI1EAfyw-T3YtVqtGMxrpflL_Wj-rU8euoxbugrJVYVtk29NI9nXeWJzobKuH-7YFqt7N8KisRG3aC_HkLVltc0bdIsNeq7bcYn/s1600-h/2_allRGBv2_HSL.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;200&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhDHqag23pojoFAG6WzmNbCDI9FC2I-rK-VdfyAHB0TAuI1EAfyw-T3YtVqtGMxrpflL_Wj-rU8euoxbugrJVYVtk29NI9nXeWJzobKuH-7YFqt7N8KisRG3aC_HkLVltc0bdIsNeq7bcYn/s200/2_allRGBv2_HSL.png&quot; width=&quot;200&quot; /&gt;&lt;/a&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiFrcjkEOzjNBKxqB542NNhADkV1tG56tVohSiNbtNRxr6gIMQGw8FshUfT4Za_xxue9E5JaOWPmT8ymsguNECfPoHxEopBzUPwdc12y5J2GvGzwVCDbgmO92YOYAmXtm2B4DBLjDaYpezr/s1600-h/2_allRGBv2_HSV.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;200&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiFrcjkEOzjNBKxqB542NNhADkV1tG56tVohSiNbtNRxr6gIMQGw8FshUfT4Za_xxue9E5JaOWPmT8ymsguNECfPoHxEopBzUPwdc12y5J2GvGzwVCDbgmO92YOYAmXtm2B4DBLjDaYpezr/s200/2_allRGBv2_HSV.png&quot; width=&quot;200&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div&gt;&lt;div style=&quot;float: left; text-align: center; width: 200px;&quot;&gt;HSL&lt;/div&gt;&lt;div style=&quot;float: right; text-align: center; width: 200px;&quot;&gt;HSV&lt;/div&gt;&lt;/div&gt;&lt;br /&gt;
&lt;p&gt;As you can see, HSL and HSV give essentially the same results, which are both much better than RGB (look closely at the wheel wells, or the buildings in the trees on the right to see the differences). I like to think that HSV is slightly better, but I might be imagining differences that really aren’t there. Either way, I chose to use HSV for the final copy. &lt;/p&gt;&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;http://allrgb.com/barcelona&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjv0v_Fpb9sKglVAH0F3OqtRYDRHeRdNN02yrcmIjZvKwCX6DKQ7LCewJi_oFSbEPF3Et59rjfbe7c2NNfwxxzEvnze2V4Z5Avbd1WpjdA6jDBwijuEdYnmsV3aF3PAXH_tl7ulxdUQg0uP/s320/2_allRGBv2_big_downsampled.png&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;
&lt;p&gt;Looks good! Certainly a lot closer to the source image – I’m satisfied with this one for now. &lt;/p&gt;&lt;h2&gt;Postscript&lt;/h2&gt;&lt;p&gt;As with last time I am using a conceptually simple algorithm, however this time the implementation was considerably more difficult. The problem is that choosing the closest remaining colour to a source pixel is a hard problem to do efficiently, especially since the set of candidate colours changes at every step. I wrote the code in C# for performance this time, but I have still had to spend quite a few hours optimizing the code to get the program to finish at all. The final version can take 30+ hours to generate an image, and peak at over 4 GB of ram. I based my code around a &lt;a href=&quot;http://www.cs.wlu.edu/%7Elevy/software/kd/&quot;&gt;KD-tree I found&lt;/a&gt; online, then rewrote to optimize for the 3D, single-nearest-neighbour case as well as to support branch pruning on delete. The rewritten tree – as well as the rest of my code – is available in a repository on GitHub: &lt;a href=&quot;https://github.com/EricBurnett/AllRGBv2&quot;&gt;http://github.com/EricBurnett/AllRGBv2&lt;/a&gt;. Feel free to try it out for yourself - if you do, I’d love to hear about it!&lt;/p&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.thelowlyprogrammer.com/feeds/8407591851905561974/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.thelowlyprogrammer.com/2010/02/visualizing-rgb-take-two.html#comment-form' title='9 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1806360094658697411/posts/default/8407591851905561974'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1806360094658697411/posts/default/8407591851905561974'/><link rel='alternate' type='text/html' href='http://www.thelowlyprogrammer.com/2010/02/visualizing-rgb-take-two.html' title='Visualizing RGB, take two'/><author><name>Eric Burnett</name><uri>http://www.blogger.com/profile/10741882872804697111</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjv0v_Fpb9sKglVAH0F3OqtRYDRHeRdNN02yrcmIjZvKwCX6DKQ7LCewJi_oFSbEPF3Et59rjfbe7c2NNfwxxzEvnze2V4Z5Avbd1WpjdA6jDBwijuEdYnmsV3aF3PAXH_tl7ulxdUQg0uP/s72-c/2_allRGBv2_big_downsampled.png" height="72" width="72"/><thr:total>9</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1806360094658697411.post-2507128517942818763</id><published>2010-02-12T19:45:00.000-05:00</published><updated>2010-02-12T19:45:15.875-05:00</updated><title type='text'>Buzz: The perfect spam platform?</title><content type='html'>Buzz is out, genius or a privacy fiasco or just another me-too, depending on your view. Those topics have been covered to death already, but one thing I haven&#39;t seen talked about is how easy it makes spamming. And I don&#39;t mean shouting inanities to all your friends - that&#39;s what it&#39;s for, after all - I&#39;m talking about targeted spam by spammers, like the blog spam that used to be a huge problem.&lt;br /&gt;
&lt;br /&gt;
Here is the problem as I see it:&lt;br /&gt;
&lt;ol&gt;&lt;li&gt;You can follow anyone you want simply by adding their email address.&lt;/li&gt;
&lt;li&gt;When they &quot;buzz&quot;, you are notified.&lt;/li&gt;
&lt;li&gt;When you comment, &lt;i&gt;they see your response&lt;/i&gt;.&lt;/li&gt;
&lt;/ol&gt;&lt;br /&gt;
Does this seem like a bad idea to anyone else? I have a feeling that Google is going to have to allow people to &quot;accept&quot; followers, bringing it that much closer to Facebook.</content><link rel='replies' type='application/atom+xml' href='http://www.thelowlyprogrammer.com/feeds/2507128517942818763/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.thelowlyprogrammer.com/2010/02/buzz-perfect-spam-platform.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1806360094658697411/posts/default/2507128517942818763'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1806360094658697411/posts/default/2507128517942818763'/><link rel='alternate' type='text/html' href='http://www.thelowlyprogrammer.com/2010/02/buzz-perfect-spam-platform.html' title='Buzz: The perfect spam platform?'/><author><name>Eric Burnett</name><uri>http://www.blogger.com/profile/10741882872804697111</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1806360094658697411.post-3251657052186216410</id><published>2010-02-12T15:31:00.001-05:00</published><updated>2021-07-17T22:41:14.608-04:00</updated><title type='text'>Straining the limits of C#</title><content type='html'>&lt;p&gt;Two weeks ago I &lt;a href=&quot;http://www.thelowlyprogrammer.com/2010/01/visualizing-rgb.html&quot;&gt;wrote about&lt;/a&gt; an algorithm to generate All-RGB images from pictures. I am currently working on a follow-up post about a new algorithm, in C# this time. This one is a bit more computationally intensive, and despite the language change it is running into scaling issues. So while I wait for it to finish, I thought I&#39;d write about a few of them.&lt;/p&gt;&lt;h2&gt;Good data structures are hard to find&lt;/h2&gt;&lt;p&gt;When you start processing large numbers of items in different ways, choosing the right data structure to store them becomes an absolute necessity. They can mean the difference between an O(n&amp;#8729;log(n)) and an O(n&lt;sup&gt;2&lt;/sup&gt;) and algorithm, which can be the difference between taking 1 hour to run or 100 years. For this project, the requirements were simple – a data structure mapping points to objects that supported nearest-neighbour searches and deletion. To me, that immediately translated to &lt;a href=&quot;https://en.wikipedia.org/wiki/Kd-tree&quot;&gt;kd-tree&lt;/a&gt;.&lt;/p&gt;&lt;br /&gt;
&lt;p&gt;Usually in cases like this I end up needing to roll my own structure, but this time I was lucky. After some Googling I found exactly &lt;a href=&quot;http://www.cs.wlu.edu/~levy/software/kd/&quot;&gt;one&lt;/a&gt; viable implementation to use, and better yet, it was open source. I&#39;m glad it was; it turned out later that there was a bug&lt;sup&gt;&lt;a href=&quot;#KDTreeBug&quot;&gt;1&lt;/a&gt;&lt;/sup&gt; that needed fixing, and I needed to compile a 64-bit version anyways (I wonder if there&#39;s a lesson in here?). It is unfortunate that this was the only option, however. I mean, there are a ton of data structure libraries for most languages you can imagine, but the vast majority of them implement the same set of structures, are buggy, unsupported, and incompatible. I would love to see a Stack Overflow-style site to address this – community edited and supported code, implementations ranked together for comparison, searching by requirements if you don&#39;t know what you need, and the list goes on.&lt;/p&gt;&lt;br /&gt;
&lt;p&gt;But even with the appropriate structure, the algorithm I have chosen will take more than a day to run and  4+ GB of memory. That is fine, I knew the approximate complexity when I started, but it does lead to the next set of issues.&lt;/p&gt;&lt;h2&gt;Good algorithms are hard to find&lt;/h2&gt;&lt;p&gt;Or should I say, good implementations of algorithms are hard to find. By way of introduction, a brief digression: my computer is unstable. Not terribly unstable, not enough to for me to actually take the time to fix it, but my sound card is slightly unsupported on Windows 7 so every once in a blue moon something like skipping a song will blue-screen the computer. Just about all my programs know how to pick up where they left off, but of course that doesn&#39;t hold for these projects I throw together in an evening. So when my computer decided to crash this morning, I decided to add some basic checkpointing. Checkpointing is easy, right? Hah!&lt;/p&gt;&lt;br /&gt;
&lt;p&gt;&lt;u&gt;Attempt 1&lt;/u&gt;: tag classes with [Serializable], run needed structures through a BinaryFormatter, streaming to file.&lt;/p&gt;&lt;p&gt;So, anyone want to guess what the problem is here? If you said object-graph size, you&#39;re right on the money. BinaryFormatter doesn&#39;t support object graphs with more than 6M items or so, and arrays get no special treatment. So serializing an array of 16.7M items throws a very unhelpful error message (&amp;quot;The internal array cannot expand to greater than Int32.MaxValue elements&amp;quot;)&lt;sup&gt;&lt;a href=&quot;#BinaryFormatterBug&quot;&gt;2&lt;/a&gt;&lt;/sup&gt;. Fine, I can unwrap my own arrays easily enough.&lt;/p&gt;&lt;br /&gt;
&lt;p&gt;&lt;u&gt;Attempt 2&lt;/u&gt;: unwrap arrays manually.&lt;/p&gt;&lt;p&gt;With each array element being serialized as a separate object, the overhead in the file is huge. If I had to guess, I&#39;d say that the size on disk is about 10 times the size in memory. And since I&#39;m trying to write about 1 GB of data...you can probably guess where this is going. Something in the output stack explodes when more than 4 GB of data is written, a number suspiciously close to the max size of an Int32. This is simply poor implementation, since it&#39;s not like I&#39;m trying to mmap the data, and large files have been supported in all modern OS&#39; for years. Not a big deal though, that data is going to be very redundant and I/O is expensive, so writing a compressed stream is probably faster in the long run.&lt;/p&gt;&lt;br /&gt;
&lt;p&gt;&lt;u&gt;Attempt 3&lt;/u&gt;: write to the file using a System.IO.Compression.GZipStream wrapper.&lt;/p&gt;&lt;p&gt;With compressed data, I expect the on-disk size to be comparable to the in-memory size, or a bit better. So the 4 GB limit should be no, problem, right? Wrong! The GZipStream has the same problem, and refuses to support more than 4 GB uncompressed. The fix here is even simpler – swap in a better GZip library.&lt;/p&gt;&lt;br /&gt;
&lt;p&gt;&lt;u&gt;Attempt 4&lt;/u&gt;: write to the file using a &lt;a href=http://www.icsharpcode.net/OpenSource/SharpZipLib/&gt;SharpZipLib&lt;/a&gt;.GZipOutputStream wrapper.&lt;/p&gt;&lt;p&gt;Success! The output file is about 700 MB and takes somewhere around 20 minutes to write, for a compression rate of about 9 MB/sec and space savings of about 93%.&lt;/p&gt;&lt;br /&gt;
&lt;p&gt;Now, I could chalk these problems up as a failing of C#, but that wouldn&#39;t be accurate. By playing with this much data I am working outside the limits expected by the library designers, and I know it. I have focused on C#, but the issues are far more general than that – I can&#39;t even find a 64-bit version of python 2.6 for Windows to test with at all, but I&#39;m sure I would run into a different set of problems if I could use it, and the same goes for the rest of the languages out there. The real issue is that versatile algorithm implementation is &lt;i&gt;hard&lt;/i&gt;, and not getting much easier with time. And that I don&#39;t have a workaround for.&lt;/p&gt;&lt;h2&gt;&lt;span style=&quot;color: rgb(102, 102, 102);&quot; &gt;&lt;br /&gt;
Footnotes&lt;/span&gt;&lt;/h2&gt;&lt;p&gt;&lt;ol&gt;&lt;li&gt;&lt;a name=KDTreeBug&gt;&lt;/a&gt;The problem is that &amp;quot;deletions&amp;quot; are supported by tombstoning, so you periodically have to rebuild the index to clean them up. That is fine, except the range() method used to get the current entries out doesn&#39;t check the deleted flag! Don&#39;t worry, I&#39;ll be sending a fix back upstream.&lt;/li&gt;
&lt;li&gt;&lt;a name=BinaryFormatterBug&gt;&lt;/a&gt;Someone else did the digging, and it seems the list of prime sizes for some internal hash table stops at 6 million, so the next prime it tries to resize to is something enormous (-1 unsigned?). Microsoft decided this is a non-issue, so no fix is coming. Their suggested workaround was to use the NetDataContractSerializer and a binary xml writer, but when I tested it the performance was too terrible to consider.&lt;/li&gt;
&lt;/ol&gt;&lt;/p&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.thelowlyprogrammer.com/feeds/3251657052186216410/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.thelowlyprogrammer.com/2010/02/straining-limits-of-c.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1806360094658697411/posts/default/3251657052186216410'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1806360094658697411/posts/default/3251657052186216410'/><link rel='alternate' type='text/html' href='http://www.thelowlyprogrammer.com/2010/02/straining-limits-of-c.html' title='Straining the limits of C#'/><author><name>Eric Burnett</name><uri>http://www.blogger.com/profile/10741882872804697111</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1806360094658697411.post-3628252323629566646</id><published>2010-01-31T02:50:00.009-05:00</published><updated>2021-07-17T22:39:23.769-04:00</updated><title type='text'>Visualizing RGB</title><content type='html'>&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;http://allrgb.com/mandelbrot&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhSqtyg_qgjbtI2YcgCPviRRTYcprDsScd2SbHFA01OE4PRgdZbHuOKyJEXYrCilAE3g9uZ8BFe1pJLif1Z32M_7yFy0vPSDbkKn_AjHpaOubpRrpWR5ahmLKMsBGxwe7dRpMeRGQypiabc/s320/10_allrgbify_downsampled.png&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;
&lt;b&gt;Update:&lt;/b&gt; See the follow-up post &lt;a href=http://www.thelowlyprogrammer.com/2010/02/visualizing-rgb-take-two.html&gt;here&lt;/a&gt;.&lt;br /&gt;
&lt;br /&gt;
&lt;a href=&quot;http://corte.si/index.html&quot;&gt;Aldo Cortesi&lt;/a&gt; posted a link today to &lt;a href=&quot;http://allrgb.com/&quot;&gt;allrgb.com&lt;/a&gt;, a site dedicated to images visualizing the RGB colour space - in particular, 4096x4096 images that use each RGB value exactly once. Inspired by his &lt;a href=&quot;http://corte.si/posts/code/hilbert/portrait/index.html&quot;&gt;hilbert curve visualization&lt;/a&gt; and the urge to spend a day programming, I present to you: the all-RGB Mandelbrot set.&lt;br /&gt;
&lt;h2&gt;Sort by colour...&lt;/h2&gt;My idea was this: instead of trying to visualize the colour space directly, why not use a base image for the &quot;shape&quot;, and then map the RGB spectrum onto it? I thought that if I could find an image with an even spread of colours, this would let me make each pixel unique yet keep the overall look untouched.&lt;br /&gt;
To perform this mapping, I chose to define an ordering based on the 3 dimensional Hilbert curve. &lt;a href=&quot;http://corte.si/posts/code/hilbert/portrait/index.html&quot;&gt;Cortesi explains it&lt;/a&gt; far better than I can, but the basic idea is this: the Hilbert curve can be used to find an ordering of all 16.8 million colours so that if you were to stretch them out on a line, every colour would be there and they would flow smoothly from one to another. Like this, except a lot smoother and a &lt;i&gt;lot&lt;/i&gt; longer.&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEio9Quu4IpDhOislw1xPVea8NZk9ianxbiPxpq0P1ekaGB76Bj7ejadnljyuXCrD5aJgFLzaIhQ39yBRa6RJACrmx_mADFAtgH_hswQPOsHXVXJLF-9O7reHmeqPYEyj2NZ-v_dHFtVrRHG/s320/hilbertLine.png&quot; /&gt;&lt;/div&gt;With this ordering in hand it is easy to find the index into this line for each pixel in the source image, sort the pixels, and then assign them the colour they line up with.&lt;br /&gt;
&lt;h2&gt;Choosing an image&lt;/h2&gt;When I started this morning, I had the idea that the output images would look reasonably close to the source images. I was half right; the images certainly have all the same features as before, but the colouring is all wrong. In hindsight the reason is obvious - unless the original had a perfectly even spectrum of colours, the mapping would be stretched in some places and shifted in others, and in general not line up nicely.&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhdstMnzqAYdy8qkoK_1ZrVhQTXbOdP_x2ab1OQ0D3ANJ9LHPIsGRfysc2j57qWx_P3bkPULyOYmidqJB_A4SDrUpMt0vKrAiL1KDZUIodmN9EOoGAi9pQzpBi2FPywdwvzuZxCZmLh5UVx/s1600-h/2_small.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;200&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhdstMnzqAYdy8qkoK_1ZrVhQTXbOdP_x2ab1OQ0D3ANJ9LHPIsGRfysc2j57qWx_P3bkPULyOYmidqJB_A4SDrUpMt0vKrAiL1KDZUIodmN9EOoGAi9pQzpBi2FPywdwvzuZxCZmLh5UVx/s200/2_small.png&quot; width=&quot;200&quot; /&gt;&lt;/a&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgh-h4lod0I53U8-LDs58TO0KFl1_JfMrhg6Brv-QFl4WKYSUKpvY53G6kyxX-loDcU3z_risMJz0Tlc7vKYnw-cwm8BG9eJLTwExv7ItD6wrx9EmbpVaKRSLUwN7wPNFGmn1dBh7o20rNc/s1600-h/2_allrgbify_small.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;200&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgh-h4lod0I53U8-LDs58TO0KFl1_JfMrhg6Brv-QFl4WKYSUKpvY53G6kyxX-loDcU3z_risMJz0Tlc7vKYnw-cwm8BG9eJLTwExv7ItD6wrx9EmbpVaKRSLUwN7wPNFGmn1dBh7o20rNc/s200/2_allrgbify_small.png&quot; width=&quot;200&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;
While interesting, this wasn&#39;t exactly what I was going for. Hmm...what image could I use where it wouldn&#39;t matter if the colours were all shifted? The first thing that came to mind was a visualization like the Mandelbrot set, where the colours are arbitrarily chosen anyways. A quick Google search found me &lt;a href=&quot;http://gwydir.demon.co.uk/jo/numbers/interest/i.htm#mandel&quot;&gt;this&lt;/a&gt;:&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;http://gwydir.demon.co.uk/jo/numbers/interest/i.htm#mandel&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;233&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhZYGVVuSvzlJWwb2i8qoyqkLLXB8vvkEFJ0cq5teuSg2lW0j46h6i8Zrs2ujCY3HVRM7tmqHtDpGeede87OjxuYWCed37h7DLUjZ4Qc3pv27okf7atc6aA3v1LyxApINDvYuQwaX1qzab_/s400/mandelbrot.jpg&quot; width=&quot;400&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;
&lt;br /&gt;
Which, when transformed, came out as this:&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;http://allrgb.com/mandelbrot&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhSqtyg_qgjbtI2YcgCPviRRTYcprDsScd2SbHFA01OE4PRgdZbHuOKyJEXYrCilAE3g9uZ8BFe1pJLif1Z32M_7yFy0vPSDbkKn_AjHpaOubpRrpWR5ahmLKMsBGxwe7dRpMeRGQypiabc/s320/10_allrgbify_downsampled.png&quot; /&gt;&lt;/a&gt;&lt;/div&gt;Perfect!&lt;br /&gt;
&lt;h2&gt;Postscript&lt;/h2&gt;I created all the visualizations here using Python 2.6 and the Python Imaging Library. It isn&#39;t the most efficient code (it takes half an hour to render a 4096x4096 image), but the quick development time easily makes up for it. If anyone is interested in playing with it, I have placed the code &lt;a href=https://gist.github.com/300780&gt;on github&lt;/a&gt;. Or if you just want to see what something would look like, feel free to &lt;a href=&quot;mailto:ericburnett@gmail.com&quot;&gt;send it to me&lt;/a&gt; and I&#39;d be happy to run it for you.</content><link rel='replies' type='application/atom+xml' href='http://www.thelowlyprogrammer.com/feeds/3628252323629566646/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.thelowlyprogrammer.com/2010/01/visualizing-rgb.html#comment-form' title='6 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1806360094658697411/posts/default/3628252323629566646'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1806360094658697411/posts/default/3628252323629566646'/><link rel='alternate' type='text/html' href='http://www.thelowlyprogrammer.com/2010/01/visualizing-rgb.html' title='Visualizing RGB'/><author><name>Eric Burnett</name><uri>http://www.blogger.com/profile/10741882872804697111</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhSqtyg_qgjbtI2YcgCPviRRTYcprDsScd2SbHFA01OE4PRgdZbHuOKyJEXYrCilAE3g9uZ8BFe1pJLif1Z32M_7yFy0vPSDbkKn_AjHpaOubpRrpWR5ahmLKMsBGxwe7dRpMeRGQypiabc/s72-c/10_allrgbify_downsampled.png" height="72" width="72"/><thr:total>6</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1806360094658697411.post-2872458671001020305</id><published>2010-01-30T14:22:00.001-05:00</published><updated>2021-07-17T22:27:24.474-04:00</updated><title type='text'>Moved!</title><content type='html'>&lt;p&gt;This blog has moved! It now lives at &lt;a href=https://www.thelowlyprogrammer.com&gt;www.thelowlyprogrammer.com&lt;/a&gt;. As is probably obvious, I have also changed the name to &lt;i&gt;The Lowly Programmer&lt;/i&gt; (see the updated &lt;a href=https://www.thelowlyprogrammer.com/2008/05/about-me.html&gt;about me&lt;/a&gt; for details). Nothing else is changing at the moment; in particular, I doubt I will be posting to it with any regularity yet :).&lt;br /&gt;
&lt;/p&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.thelowlyprogrammer.com/feeds/2872458671001020305/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.thelowlyprogrammer.com/2010/01/moved.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1806360094658697411/posts/default/2872458671001020305'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1806360094658697411/posts/default/2872458671001020305'/><link rel='alternate' type='text/html' href='http://www.thelowlyprogrammer.com/2010/01/moved.html' title='Moved!'/><author><name>Eric Burnett</name><uri>http://www.blogger.com/profile/10741882872804697111</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1806360094658697411.post-2343174528989396499</id><published>2010-01-16T16:29:00.001-05:00</published><updated>2021-07-17T22:27:12.055-04:00</updated><title type='text'>The value of a point</title><content type='html'>&lt;span xmlns=&#39;&#39;&gt;&lt;p&gt;Recently I have been spending a lot of time on &lt;a href=&quot;https://news.ycombinator.com/&quot;&gt;Hacker News&lt;/a&gt;. I&#39;ve lurked for quite a while, but it is only in the last month or so that I have actively started to comment. I usually try for informative comments rather than going for the inflammatory or popular topics. This only gets me a &lt;a href=&quot;http://www.randsinrepose.com/archives/2009/12/13/gaming_the_system.html&quot;&gt;point&lt;/a&gt; or two per comment I post, but these are points I have &lt;i&gt;earned&lt;/i&gt; by contributing to the discussion in a positive way. I had almost reached 50 points this morning and was feeling quite good about my contributions overall.&lt;br /&gt;
&lt;/p&gt;&lt;p&gt;But then I made a submission.&lt;br /&gt;
&lt;/p&gt;&lt;p&gt;Most of my submissions go the way of my comments, earning with a point or two plus some interesting responses. It&#39;s these responses I am after; I find an interesting article somewhere, so I submit it to see what the HN community has to say about it. But &lt;a href=&quot;https://news.ycombinator.com/item?id=1057133&quot;&gt;this one&lt;/a&gt;, for whatever reason, the community really appreciated, and within 20 minutes it was at the top of the front page. The discussion was lively and as interesting to read as I could hope for, and it was raking in the points.&lt;br /&gt;
&lt;/p&gt;&lt;p&gt;And therein lies the problem. Someone, a self proclaimed member of HN no less, took the time and effort to write a thought provoking piece and there I was earning scores of points off it, which left me feeling like a bit of a cheat. But even had I been the original author, I don&#39;t think I would want that many points for it. By the time it falls off the front page it will likely be over 50 points itself, surpassing a month&#39;s effort in one fell swoop and devaluing the points accordingly. I was looking at points as a general reflection of my value to the site, but I cannot do that anymore, at least not directly.&lt;br /&gt;
&lt;/p&gt;&lt;p&gt;So what can be done about the &#39;problem&#39; with points? I can think of a few suggestions.&lt;br /&gt;
&lt;ol start=&quot;1&quot; style=&quot;margin-top: 0in;&quot; type=&quot;1&quot;&gt;&lt;li&gt;Nothing. After all, I am only a single opinion, and the system seems to be working fine as it is. &lt;/li&gt;
&lt;li&gt;Don&#39;t count any points past a certain cutoff. They would still be useful for display and ranking, but for karma the extra would be discarded. I kind of like this idea, but any cutoff would be an arbitrary one. &lt;a href=&quot;https://stackoverflow.com/&quot;&gt;Stack Overflow&lt;/a&gt; uses a system like this, although there it&#39;s points per day rather than per action.&lt;/li&gt;
&lt;li&gt;Count points on a log scale. Display them as normal, but change the way karma is calculated. This would help address crazy outliers like &lt;a href=&quot;https://news.ycombinator.com/item?id=1048800&quot;&gt;this one&lt;/a&gt;, and in general promote sustained quality over hot-button topics (for people actively trying for points).&lt;/li&gt;
&lt;/ol&gt;I know &lt;a href=&quot;http://www.paulgraham.com/&quot;&gt;pg&lt;/a&gt; is always tweaking the system to make it work better; it will be interesting to see what, if anything, actually changes in the next while.&lt;/p&gt;&lt;/span&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.thelowlyprogrammer.com/feeds/2343174528989396499/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.thelowlyprogrammer.com/2010/01/value-of-point.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1806360094658697411/posts/default/2343174528989396499'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1806360094658697411/posts/default/2343174528989396499'/><link rel='alternate' type='text/html' href='http://www.thelowlyprogrammer.com/2010/01/value-of-point.html' title='The value of a point'/><author><name>Eric Burnett</name><uri>http://www.blogger.com/profile/10741882872804697111</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1806360094658697411.post-6239963055035128102</id><published>2009-06-06T04:13:00.001-04:00</published><updated>2009-06-06T04:14:36.278-04:00</updated><title type='text'>Google Wave. Can you imagine?</title><content type='html'>&lt;span xmlns=&#39;&#39;&gt;&lt;p&gt;I&#39;m sure you&#39;ve all heard of &lt;a href=&#39;http://wave.google.com/&#39;&gt;Google Wave&lt;/a&gt; by now, but for those who haven&#39;t, think of a hybrid email-chat-collaboration program all rolled into one. Participants can message back and forth on their own time like email, but it&#39;s also much more than that. As you type, your keystrokes are streamed in real-time to everyone else, letting them read what you write &lt;em&gt;as you write it&lt;/em&gt;. Couple that with the ability to comment and edit others&#39; comments, and what do you get? I&#39;m not sure, but we&#39;ll find out soon enough.&lt;br /&gt;&lt;/p&gt;&lt;p&gt;I don&#39;t have any earth-shattering insights about how this product is going to revolutionize how we communicate (or fail completely), but I would like you to consider one scenario. Imagine a group of Digg (or 4chan or reddit or...) users, all getting onto a wave at once. They&#39;d be able to fight for the top, change each others&#39; comments to include &lt;em&gt;even more memes&lt;/em&gt;, spam, complain, and of course mock, and all in real-time. Pedobears would be flying, grammar would be mutilated in the name of speed, and still people would be replying in anger to posts not even finished. Would it be fun? Quite possibly. Carnage? Undoubtedly.&lt;br /&gt;&lt;/p&gt;&lt;p&gt;Welcome to the internet, Wave.&lt;/p&gt;&lt;/span&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.thelowlyprogrammer.com/feeds/6239963055035128102/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.thelowlyprogrammer.com/2009/06/google-wave-can-you-imagine.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1806360094658697411/posts/default/6239963055035128102'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1806360094658697411/posts/default/6239963055035128102'/><link rel='alternate' type='text/html' href='http://www.thelowlyprogrammer.com/2009/06/google-wave-can-you-imagine.html' title='Google Wave. Can you imagine?'/><author><name>Eric Burnett</name><uri>http://www.blogger.com/profile/10741882872804697111</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1806360094658697411.post-2404775663018489669</id><published>2008-09-17T20:10:00.002-04:00</published><updated>2008-09-17T20:13:16.305-04:00</updated><title type='text'>How fast do you type?</title><content type='html'>&lt;span xmlns=&#39;&#39;&gt;&lt;p&gt;According to &lt;a href=&#39;http://www.typingtest.com&#39;&gt;typingtest.com&lt;/a&gt;, I can type at 58 words per minute with 97% accuracy, which is probably about average among my roommates. But that is &lt;em&gt;words&lt;/em&gt; per minute; I am a programmer, I spend as much time typing code filled with hyphens and tildes and ampersands as typing &quot;words&quot;. And I must admit, I never learned to touch-type type numbers or symbols, so typing code involves a lot of glancing down at the keyboard to find out exactly where a symbol is. Try it yourself – here is a string I find myself typing fairly often during a day at work (and no, it isn&#39;t confidential!):&lt;br /&gt;&lt;/p&gt;&lt;blockquote&gt;&lt;p style=&quot;font-family: sans-serif&quot;&gt;dbisql.exe –c &quot;UID=dba;PWD=123;ENG=myEngine;DBF=..\testdb.db&quot;&lt;br /&gt;&lt;/p&gt;&lt;/blockquote&gt;&lt;p&gt;How quick can you type that? And I will point out that this string doesn&#39;t even have any really difficult characters, just lots of punctuation. &lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;a href=&#39;http://steve-yegge.blogspot.com&#39;&gt;Steve Yegge&lt;/a&gt; wrote a really good rant recently on the subject of typing (or as he calls it, &lt;a href=&#39;http://steve-yegge.blogspot.com/2008/09/programmings-dirtiest-little-secret.html&#39;&gt;Programming&#39;s Dirtiest Little Secret&lt;/a&gt;), and it got me thinking. As good as I am at typing normal text, I fall apart when I run into anything else. Before reading his rant, I would never have thought of putting practicing typing on my professional development list, despite the fact that it will serve me better than any of the books I&#39;d read or languages I&#39;d learn. But right now I am going to put learning python on hold (again) and spend some quality time with my old friend Mavis Beacon.&lt;/p&gt;&lt;/span&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.thelowlyprogrammer.com/feeds/2404775663018489669/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.thelowlyprogrammer.com/2008/09/how-fast-do-you-type.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1806360094658697411/posts/default/2404775663018489669'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1806360094658697411/posts/default/2404775663018489669'/><link rel='alternate' type='text/html' href='http://www.thelowlyprogrammer.com/2008/09/how-fast-do-you-type.html' title='How fast do you type?'/><author><name>Eric Burnett</name><uri>http://www.blogger.com/profile/10741882872804697111</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1806360094658697411.post-1658695802214436730</id><published>2008-05-31T18:50:00.004-04:00</published><updated>2008-05-31T19:20:37.627-04:00</updated><title type='text'>What’s next for copyright?</title><content type='html'>&lt;span xmlns=&#39;&#39;&gt;&lt;p&gt;Everyone knows there is a copyright war going on right now. Big companies own the copyright to virtually all music, movies, and television shows, and they want to make large profits off these rights. People, on the other hand, want to be able to play their media when and where they want, and ideally they&#39;d like to get it for free. This conflict shows up in a number of guises – battles over internet privacy, digital rights management, and bittorrent, to name a few – and shows no signs of being resolved any time soon. I do think there is hope for a solution, but the industry needs to accept that they can&#39;t control everything.&lt;br /&gt;&lt;/p&gt;&lt;p&gt;To see it, let&#39;s take a look at one website that ignores copyright and gets away with it: YouTube. Users can upload and watch virtually anything, and due to the sheer number of uploads, copyright holders can&#39;t keep up. It survives because there is a big company protecting it from lawsuits, but that isn&#39;t why it&#39;s popular. The biggest feature here is ease of use – users go to YouTube because it is the best way to get the content they&#39;re looking for. YouTube already makes lots of money; if publishers were to learn from this, I think they&#39;d have a winning strategy.&lt;br /&gt;&lt;/p&gt;&lt;p&gt;Of course, the challenge is to turn this example into a general solution. I would say that big publishers can do it, but they need to change their business model.  What they should do is try to control the brand and be the best for distribution, to make it as easy as possible for people to get the content they want. I&#39;ll give a couple examples of how I think this could go.&lt;br /&gt;&lt;/p&gt;&lt;p&gt;I picture a website much like YouTube, but controlled by a publisher. Users can visit it to watch movies and TV shows for free, at their own leisure. To fund this they use the tried and true internet model: advertising. Most of it is general advertising, keyed to the content in the movie, and the rest is good for the publisher – collector&#39;s edition DVDs, upcoming movies with the same director, etcetera. Sure, people still pass movies around on bittorrent, but this is quick and convenient. Then persecuting people who share files isn&#39;t really necessary, since many of them will make it to the publisher&#39;s site on their own. Similarly, people won&#39;t fight as hard to protect other sites hosting the content, since there is an easy, legal, and free channel to get the same. Naturally this wouldn&#39;t be a replacement for physical media, but the shows will be on the internet regardless of what publishers want, so they may as well get a slice out of it.&lt;br /&gt;&lt;/p&gt;&lt;p&gt;For music, this could go even further. A music company could run their own download site or tracker, putting up the content themselves. It would be high quality &lt;em&gt;official&lt;/em&gt; content, for free. They could try to get users to pay if they like the music, buy physical copies, etcetera. Remind you of anything? &lt;a href=&#39;http://www.techcrunch.com/2007/10/04/the-inevitable-march-of-recorded-music-towards-free/&#39;&gt;Radiohead has done this&lt;/a&gt;. &lt;em&gt;After&lt;/em&gt; this is in place, they could go after other distributors, to keep people coming to the &#39;official&#39; site. Again, people will share the music. But who cares? It&#39;s free unless they choose to pay, and let&#39;s face it, they are going to share anyways.&lt;br /&gt;&lt;/p&gt;&lt;p&gt;In that case, why would this kind of scheme work? A number of reasons:&lt;br /&gt;&lt;/p&gt;&lt;ul&gt;&lt;li&gt;It is easy to do. Laws don&#39;t need to be changed, and people won&#39;t fight it.&lt;br /&gt;&lt;/li&gt;&lt;li&gt;Profits are still good. Maybe not as high as they once were, but that industry is dying anyways. And compared to physical media, costs are virtually non-existent, so people wouldn&#39;t need to pay nearly as much piece of media for this to work.&lt;br /&gt;&lt;/li&gt;&lt;li&gt;Publishers have already lost control; they just don&#39;t know it yet. If you can&#39;t control your users, make them &lt;em&gt;want&lt;/em&gt; to come to you.&lt;br /&gt;&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;There are lots of signs that this kind of model is coming. The biggest problem is that it isn&#39;t being done by the publishers. Either artists are breaking off and trying it themselves, or companies are springing up to sign deals and try it for themselves. Somehow we need to get the publishers to try it, and I think the whole problem of draconian copyright enforcement will slowly disappear. Or at least, it won&#39;t be the users themselves getting slapped with lawsuits.&lt;/p&gt;&lt;/span&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.thelowlyprogrammer.com/feeds/1658695802214436730/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.thelowlyprogrammer.com/2008/05/whats-next-for-copyright.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1806360094658697411/posts/default/1658695802214436730'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1806360094658697411/posts/default/1658695802214436730'/><link rel='alternate' type='text/html' href='http://www.thelowlyprogrammer.com/2008/05/whats-next-for-copyright.html' title='What’s next for copyright?'/><author><name>Eric Burnett</name><uri>http://www.blogger.com/profile/10741882872804697111</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1806360094658697411.post-4189459878832255218</id><published>2008-05-29T22:16:00.005-04:00</published><updated>2012-08-16T04:14:15.436-04:00</updated><title type='text'>NOT Puzzle Solution</title><content type='html'>&lt;p&gt;&lt;i&gt;&lt;strong&gt;Edited 2012/08/16:&lt;/strong&gt; Updated problem attribution.&lt;/i&gt;&lt;/p&gt;&lt;p&gt;Last week, I closed with a puzzle: &quot;Invert three inputs using only two NOT gates.&quot; I should start off by pointing out that I cannot take credit for this problem; it dates back to at least the 70s and was &lt;a href=&quot;http://www.inwap.com/pdp10/hbaker/hakmem/boolean.html#item19&quot;&gt;featured in HAKMEM&lt;/a&gt; by Richard Schroeppel. I myself learned about it via &lt;a href=&quot;http://www.pldesignline.com/187202855&quot;&gt;Clive Maxfield&lt;/a&gt;. I&#39;m also told that Guy Steele beat me to the generalization to &lt;strong&gt;n&lt;/strong&gt; case, however I cannot verify this, and the solution presented here is entirely my own.&lt;/p&gt;&lt;p&gt;The solutions that follow are just two of many possible ways of looking at this. Structurally, they are by no means the simplest solutions, but I find them to be &lt;em&gt;conceptually&lt;/em&gt; the simplest. If you haven&#39;t tried to solve the puzzle yet I heartily recommend it; it makes knowing the solution so much more rewarding if you have.&lt;/p&gt;&lt;h2&gt;Solution for 3 inputs&lt;/h2&gt;&lt;p&gt;My solution to this puzzle involves reasoning on the number of inputs with value &#39;1&#39;. If none of the inputs are 1s, then all the outputs must be, and vice versa. To do this, we create a set of temporary variables with the goal of knowing exactly how many inputs were 1.&lt;/p&gt;&lt;blockquote&gt;&lt;p style=&quot;font-family: sans-serif&quot;&gt;3 := A &amp;and; B &amp;and; C&lt;br /&gt;
2or3 := (A &amp;and; B) &amp;or; (B &amp;and; C) &amp;or; (A &amp;and; C)&lt;br /&gt;
0or1 := ¬ 2or3&lt;br /&gt;
1 := 0or1 &amp;and; (A &amp;or; B &amp;or; C)&lt;br /&gt;
0or2 := ¬ (1 &amp;or; 3)&lt;br /&gt;
0 := 0or1 &amp;and; 0or2&lt;br /&gt;
2 := 0or2 &amp;and; 2or3&lt;/p&gt;&lt;/blockquote&gt;&lt;p&gt;From here we can just build our outputs directly:&lt;/p&gt;&lt;blockquote&gt;&lt;p style=&quot;font-family: sans-serif&quot;&gt;X = 0 &amp;or; (1 &amp;and; (B &amp;or; C)) &amp;or; (2 &amp;and; (B &amp;and; C))&lt;br /&gt;
Y = 0 &amp;or; (1 &amp;and; (A &amp;or; C)) &amp;or; (2 &amp;and; (A &amp;and; C))&lt;br /&gt;
Z = 0 &amp;or; (1 &amp;and; (A &amp;or; B)) &amp;or; (2 &amp;and; (A &amp;and; B))&lt;/p&gt;&lt;/blockquote&gt;&lt;h2&gt;Solution for &lt;strong&gt;n&lt;/strong&gt; inputs&lt;/h2&gt;&lt;p&gt;This proof sketch is for odd &lt;strong&gt;n. &lt;/strong&gt;If &lt;strong&gt;n&lt;/strong&gt; is even, just invert the last input directly with a NOT, and use this pattern for the first &lt;strong&gt;n&lt;/strong&gt;-1.&lt;/p&gt;&lt;p&gt;As before, we are going to reason on how many inputs (I&lt;sub&gt;1&lt;/sub&gt; to I&lt;sub&gt;n&lt;/sub&gt;) were 1. For this we will use a bit more syntax: {a-b} means anywhere from a to b inputs (inclusive) were 1, and {a, b} means either a &lt;em&gt;or&lt;/em&gt; b inputs were 1, with no other possibilities allowed. First, we can generate all the ranges ending in &lt;strong&gt;n&lt;/strong&gt; directly:&lt;/p&gt;&lt;blockquote&gt;&lt;p style=&quot;font-family: sans-serif&quot;&gt;{1-&lt;strong&gt;n&lt;/strong&gt;} := (I&lt;sub&gt;1&lt;/sub&gt; &amp;or; I&lt;sub&gt;2&lt;/sub&gt; &amp;or; … &amp;or; I&lt;sub&gt;n&lt;/sub&gt;)&lt;br /&gt;
{2-&lt;strong&gt;n&lt;/strong&gt;} := ((I&lt;sub&gt;1&lt;/sub&gt; &amp;and; I&lt;sub&gt;2&lt;/sub&gt;) &amp;or; (I&lt;sub&gt;1&lt;/sub&gt; &amp;and; I&lt;sub&gt;3&lt;/sub&gt;) &amp;or; … &amp;or; (I&lt;sub&gt;n-1&lt;/sub&gt; &amp;and; I&lt;sub&gt;n&lt;/sub&gt;))&lt;br /&gt;
…&lt;br /&gt;
{&lt;strong&gt;n&lt;/strong&gt;-&lt;strong&gt;n&lt;/strong&gt;} := (I&lt;sub&gt;1&lt;/sub&gt; &amp;and; I&lt;sub&gt;2&lt;/sub&gt; &amp;and; … &amp;and; I&lt;sub&gt;n&lt;/sub&gt;)&lt;/p&gt;&lt;/blockquote&gt;&lt;p&gt;Clearly, this doesn&#39;t need any NOTs. Next, we are going to make all the ranges starting in 0 and ending in an odd number:&lt;/p&gt;&lt;blockquote&gt;&lt;p style=&quot;font-family: sans-serif&quot;&gt;{0-1} := ¬ {2-&lt;strong&gt;n&lt;/strong&gt;}&lt;br /&gt;
{0-3} := ¬ {4-&lt;strong&gt;n&lt;/strong&gt;}&lt;br /&gt;
…&lt;br /&gt;
{0-(&lt;strong&gt;n&lt;/strong&gt;-2)} := ¬{(&lt;strong&gt;n&lt;/strong&gt;-1)-&lt;strong&gt;n&lt;/strong&gt;}&lt;/p&gt;&lt;/blockquote&gt;&lt;p&gt;This requires (&lt;strong&gt;n&lt;/strong&gt;-1)/2 NOT gates. Using these together, we can have variables for all the odd numbers of inputs directly:&lt;/p&gt;&lt;blockquote&gt;&lt;p style=&quot;font-family: sans-serif&quot;&gt;1 := {0-1} &amp;and; {1-&lt;strong&gt;n&lt;/strong&gt;}&lt;br /&gt;
3 := {0-3} &amp;and; {3-&lt;strong&gt;n&lt;/strong&gt;}&lt;br /&gt;
…&lt;br /&gt;
&lt;strong&gt;n&lt;/strong&gt;-2 := {0-(&lt;strong&gt;n&lt;/strong&gt;-2)} &amp;and; {(&lt;strong&gt;n&lt;/strong&gt;-2)-&lt;strong&gt;n&lt;/strong&gt;}&lt;br /&gt;
&lt;strong&gt;n&lt;/strong&gt; := {&lt;strong&gt;n&lt;/strong&gt;-&lt;strong&gt;n&lt;/strong&gt;}&lt;/p&gt;&lt;/blockquote&gt;&lt;p&gt;Again, this doesn&#39;t require any NOTs. Lastly, we use one more NOT to let us get all the even values:&lt;/p&gt;&lt;blockquote&gt;&lt;p style=&quot;font-family: sans-serif&quot;&gt;{0,2,4,…,(&lt;strong&gt;n&lt;/strong&gt;-1)} := ¬ (1 &amp;or; 3 &amp;or; … &amp;or; &lt;strong&gt;n&lt;/strong&gt;)&lt;br /&gt;
&lt;br /&gt;
0 := {0-1} &amp;and; {0,2,4,…,(&lt;strong&gt;n&lt;/strong&gt;-1)}&lt;br /&gt;
2 := {0-3} &amp;and; {0,2,4,…,(&lt;strong&gt;n&lt;/strong&gt;-1)}  &amp;and; {2-&lt;strong&gt;n&lt;/strong&gt;}&lt;br /&gt;
4 := {0-5} &amp;and; {0,2,4,…,(&lt;strong&gt;n&lt;/strong&gt;-1)} &amp;and; {4-&lt;strong&gt;n&lt;/strong&gt;}&lt;br /&gt;
…&lt;br /&gt;
&lt;strong&gt;n&lt;/strong&gt;-1 := {0,2,4,…,(&lt;strong&gt;n&lt;/strong&gt;-1)}  &amp;and; {(&lt;strong&gt;n&lt;/strong&gt;-1)-&lt;strong&gt;n&lt;/strong&gt;}&lt;/p&gt;&lt;/blockquote&gt;&lt;p&gt;There! Now we have a variable for each possible number of inputs with value 1, so we can just plug in all possibilities to get our outputs:&lt;/p&gt;&lt;blockquote&gt;&lt;p style=&quot;font-family: sans-serif&quot;&gt;O&lt;sub&gt;1&lt;/sub&gt; = 0 &amp;or; (1 &amp;and; (I&lt;sub&gt;2&lt;/sub&gt; &amp;or; I&lt;sub&gt;3&lt;/sub&gt; &amp;or; … &amp;or; I&lt;sub&gt;n&lt;/sub&gt;)) &amp;or; … &amp;or; (&lt;strong&gt;n&lt;/strong&gt;-1 &amp;and; (I&lt;sub&gt;2&lt;/sub&gt; &amp;and; I&lt;sub&gt;3&lt;/sub&gt; &amp;and; … &amp;and; I&lt;sub&gt;n&lt;/sub&gt;))&lt;br /&gt;
O&lt;sub&gt;2&lt;/sub&gt; = 0 &amp;or; (1 &amp;and; (I&lt;sub&gt;1&lt;/sub&gt; &amp;or; I&lt;sub&gt;3&lt;/sub&gt; &amp;or; … &amp;or; I&lt;sub&gt;n&lt;/sub&gt;)) &amp;or; … &amp;or; (&lt;strong&gt;n&lt;/strong&gt;-1 &amp;and; (I&lt;sub&gt;1&lt;/sub&gt; &amp;and; I&lt;sub&gt;3&lt;/sub&gt; &amp;and; … &amp;and; I&lt;sub&gt;n&lt;/sub&gt;))&lt;br /&gt;
…&lt;br /&gt;
O&lt;sub&gt;n&lt;/sub&gt; = 0 &amp;or; (1 &amp;and; (I&lt;sub&gt;1&lt;/sub&gt; &amp;or; I&lt;sub&gt;2&lt;/sub&gt; &amp;or; … &amp;or; I&lt;sub&gt;n-1&lt;/sub&gt;)) &amp;or; … &amp;or; (&lt;strong&gt;n&lt;/strong&gt;-1 &amp;and; (I&lt;sub&gt;1&lt;/sub&gt; &amp;and; I&lt;sub&gt;1&lt;/sub&gt; &amp;and; … &amp;and; I&lt;sub&gt;n-1&lt;/sub&gt;))&lt;/p&gt;&lt;/blockquote&gt;&lt;p&gt;As you have seen, this took a total of (&lt;strong&gt;n&lt;/strong&gt;-1)/2 + 1 NOT gates. Since &lt;strong&gt;n&lt;/strong&gt; is odd, (&lt;strong&gt;n&lt;/strong&gt;-1)/2 + 1 = &amp;lfloor;&lt;strong&gt;n&lt;/strong&gt;/2&amp;rfloor; + 1, and we&#39;re done! Whew, I wouldn&#39;t want to implement that.&lt;/p&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.thelowlyprogrammer.com/feeds/4189459878832255218/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.thelowlyprogrammer.com/2008/05/not-puzzle-solution.html#comment-form' title='5 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1806360094658697411/posts/default/4189459878832255218'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1806360094658697411/posts/default/4189459878832255218'/><link rel='alternate' type='text/html' href='http://www.thelowlyprogrammer.com/2008/05/not-puzzle-solution.html' title='NOT Puzzle Solution'/><author><name>Eric Burnett</name><uri>http://www.blogger.com/profile/10741882872804697111</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>5</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1806360094658697411.post-7869519820726152618</id><published>2008-05-24T14:54:00.004-04:00</published><updated>2021-07-17T22:26:06.721-04:00</updated><title type='text'>Boolean Logic</title><content type='html'>&lt;span xmlns=&quot;&quot;&gt;&lt;p&gt;Today we discuss Boolean Logic. It may be the first in a series on different kinds of logic, or it may not. We&#39;ll see.&lt;br /&gt;&lt;/p&gt;&lt;p&gt;A concept that should be intimately familiar to any programmer is &lt;em&gt;Boolean logic.&lt;/em&gt; True or False, 1 or 0, Yes or No, these are all different representations of a Boolean variable. Invented by George Boole in the 1800s, Boolean logic forms the basis of modern computing, used in everything from the base transistor to bitmasks and search queries. &lt;a href=&quot;https://en.wikipedia.org/wiki/Boolean_algebra&quot;&gt;Wikipedia&lt;/a&gt; has a good introduction to the theory for anyone who needs a refresher. Rather than re-teach it, here I am going to draw your attention to a few of the more interesting features of Boolean logic.&lt;br /&gt;&lt;/p&gt;&lt;p&gt;First off, everything can be defined from only one basic operation, &lt;em&gt;alternative denial&lt;/em&gt; (aka NAND, ↑). Alternatively, the same can be done with NOR, albeit in different patterns. For example, NOT can be created by splitting the input into both sides of a NAND gate. For this reason – and many others – NAND is often used as a building block of transistor circuits. It is a bit awkward for general use however, so for higher level Boolean logic we usually deal in three main operations: &lt;em&gt;negation&lt;/em&gt; (NOT, or ¬), &lt;em&gt;conjunction&lt;/em&gt; (AND, or ∧), and &lt;em&gt;disjunction&lt;/em&gt; (OR, or ∨). There are others, but these are good enough for now. Take a look at a few examples:&lt;br /&gt;&lt;/p&gt;&lt;blockquote&gt;&lt;p&gt;A ∨ B  =  ¬(¬A ∧ ¬B)      (De Morgan&#39;s Law)&lt;br /&gt;0  =  A ∧ ¬A&lt;br /&gt;1  =  ¬(A ∧ ¬A)&lt;br /&gt;&lt;/p&gt;&lt;/blockquote&gt;&lt;p&gt;Even just using these three operators, there are multiple ways to express any given function. For example, which is the &#39;best&#39; way to represent XOR (the ⊕ operator)?&lt;br /&gt;&lt;/p&gt;&lt;blockquote&gt;&lt;p&gt;A ⊕ B  =  (¬A ∧ B) ∨ (A ∧ ¬B)&lt;br /&gt;A ⊕ B  =  (A ∨ B) ∧ ¬(A ∧ B)&lt;br /&gt;&lt;/p&gt;&lt;/blockquote&gt;&lt;p&gt;The first is easier for most people to understand (&quot;A is true and B is false, or the other way around&quot;), while the second has fewer logical operations. If we wanted to implement XOR in software that might matter, although in hardware it would usually be implemented as a custom unit such as the CMOS 4070, or failing that, four NAND gates:&lt;br /&gt;&lt;/p&gt;&lt;blockquote&gt;&lt;p&gt;N  :=  A ↑ B&lt;br /&gt;A ⊕ B  =  (A ↑ N) ↑ (N ↑ B)&lt;br /&gt;&lt;/p&gt;&lt;/blockquote&gt;&lt;p&gt;&lt;span xmlns=&quot;&quot;&gt;&lt;a onblur=&quot;try {parent.deselectBloggerImageGracefully();} catch(e) {}&quot; href=&quot;https://en.wikipedia.org/wiki/Image:XOR_Using_NAND.jpg&quot;&gt;&lt;img style=&quot;cursor: pointer; display: block; margin-left: auto; margin-right: auto;&quot; src=&quot;https://upload.wikimedia.org/wikipedia/commons/f/f1/XOR_Using_NAND.jpg&quot; alt=&quot;&quot; border=&quot;0&quot; /&gt;&lt;/a&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;Another interesting feature of hardware level Boolean logic is &lt;em&gt;propagation delay&lt;/em&gt;.  Essentially, we think of logical operations as instantaneous, however in hardware they take a measured time to switch. The propagation delay is the total time it takes for a change in the inputs to be reflected as a change of the outputs of a function. This gives us a tradeoff between time and transistor count – basically, it might be better to use more transistors to implement a function, but structured in parallel. This tradeoff is sometimes reflected in software: if your computer can run multiple instructions in parallel, it is sometimes possible to use more instructions perform a function in fewer clock cycles. The compiler usually handles this kind of micro-optimization, however.&lt;br /&gt;&lt;/p&gt;&lt;p&gt;One other interesting tidbit: take a look at &lt;a href=&quot;https://en.wikipedia.org/wiki/Karnaugh_map&quot;&gt;Karnaugh maps&lt;/a&gt; for simplifying expressions and finding &lt;a href=&quot;https://en.wikipedia.org/wiki/Race_condition&quot;&gt;race conditions&lt;/a&gt;. Essentially, you draw out the truth table for an expression and then cover the &#39;1&#39;s with as few overlapping rectangles as possible, optionally adding extras to prevent race conditions.&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;span xmlns=&quot;&quot;&gt;&lt;a onblur=&quot;try {parent.deselectBloggerImageGracefully();} catch(e) {}&quot; href=&quot;https://commons.wikimedia.org/wiki/Image:K-map_6%2C8%2C9%2C10%2C11%2C12%2C13%2C14_anti-race.svg&quot;&gt;&lt;img style=&quot;cursor: pointer; display: block; margin-left: auto; margin-right: auto;&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiY8dI7cpRbY9SIrV5juJBe0U_2I3D-XKvZ3tpj2l5OfhqwYDiqxtcKqaKvSD2vtnn0VS22V0src-cmyR3qRlwlRQf_Pm1Qn33ctdbcYeGuiHnMD_iwZAMweEj1yJCpI5G_MAWJxy_QyE_p/s400/K-map_6,8,9,10,11,12,13,14_anti-race.png&quot; alt=&quot;&quot; id=&quot;BLOGGER_PHOTO_ID_5204033728254623746&quot; border=&quot;0&quot; /&gt;&lt;/a&gt;&lt;/span&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;Lastly, I&#39;ll leave you with a puzzle: invert three inputs using only two NOT gates. To be specific, you have a function with 3 inputs, A B C, and you want three outputs, X Y Z, such that&lt;br /&gt;&lt;/p&gt;&lt;blockquote&gt;&lt;p&gt;X  =  ¬A&lt;br /&gt;Y  =  ¬B&lt;br /&gt;Z  =  ¬C&lt;br /&gt;&lt;/p&gt;&lt;/blockquote&gt;&lt;p&gt;However, you only have two NOT gates (although you can use as many AND or OR gates as you&#39;d like). Note: to solve this in written form (rather than by drawing a circuit diagram), you may find that you need temporary variables, such as &#39;N := ¬A ∧ B&#39;.&lt;br /&gt;&lt;/p&gt;&lt;p&gt;Bonus points if you can generalize it to an algorithm for inverting &lt;strong&gt;n&lt;/strong&gt; inputs using ⌊&lt;strong&gt;n&lt;/strong&gt;/2⌋ + 1 NOT gates.&lt;/p&gt;&lt;/span&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.thelowlyprogrammer.com/feeds/7869519820726152618/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.thelowlyprogrammer.com/2008/05/boolean-logic.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1806360094658697411/posts/default/7869519820726152618'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1806360094658697411/posts/default/7869519820726152618'/><link rel='alternate' type='text/html' href='http://www.thelowlyprogrammer.com/2008/05/boolean-logic.html' title='Boolean Logic'/><author><name>Eric Burnett</name><uri>http://www.blogger.com/profile/10741882872804697111</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiY8dI7cpRbY9SIrV5juJBe0U_2I3D-XKvZ3tpj2l5OfhqwYDiqxtcKqaKvSD2vtnn0VS22V0src-cmyR3qRlwlRQf_Pm1Qn33ctdbcYeGuiHnMD_iwZAMweEj1yJCpI5G_MAWJxy_QyE_p/s72-c/K-map_6,8,9,10,11,12,13,14_anti-race.png" height="72" width="72"/><thr:total>0</thr:total></entry></feed>