Nostalgic prime number generator

16

Remember the good old days when opening a simple document or web page was painfully slow as it hogged all the meager resources your computer had? And today, doing the same is even slower, despite your processor being hundreds of times faster and having access to thousands of times more memory?

To simulate the effect of bloat in current document viewers and similar applications, write a program which has visible performance problems when run on more powerful machines.

To have a common task for everyone, make it a prime number generator.

  • The program has to print consecutive prime numbers, starting from 2, each in a new line, and nothing else. It should to this forever (or until running out of memory). Like this:
2
3
5
7
11
13
17
  • There should be a delay between printing each line, enough to be perceivable by a human.

  • This delay should be longer as the machine the program is running on gets faster. The faster the machine, the slower the program.

  • I won't specify exact benchmarks as it might become subjective, but there should be a human-perceivable difference in the speed on two different machines if there is significant difference between the performance of the two machines.

  • The speed of the program doesn't have to be monotonically decreasing across all existing machines ever created. This would be hard to specify, and even harder to verify. I trust the common sense of the contestants about what can be considered a significantly different performance between to machines, and it is enough to satisfy that.

  • I also won't specify exact upper or lower time limits, but it should be under reasonable limits, so no days or years between printing two lines please.

  • I won't require it to run on everything from the Eniac to modern day, but it should be general enough, for example, it's not allowed to say it only works on two specific CPU types, and specifically detects the name of one specific CPU on which it will run slower or faster.

  • The code shouldn't rely on compiler or interpreter version. It should work if the same version of the compiler/interpreter is installed on both a slower and faster machine, or even if the binary / bytecode is compiled on one machine and then run on two different machines.

  • Please explain the principles of how your program is operating. As it will be difficult to reproduce the results, the validity of the answer might depend on the the feasibility of the method.

Although I would have liked it to become an underhanded contest, sadly this site is no longer "Programming Puzzles & Code Golf" but just simply "Code Golf", so the shortest code wins.

vsz

Posted 2016-12-06T20:29:47.990

Reputation: 7 963

Question was closed 2016-12-07T20:22:10.350

"delay...perceivable" can you quantify that with at least 1/4 second or something? – Rɪᴋᴇʀ – 2016-12-06T20:40:23.527

@EasterlyIrk : the problem with exact specifications (the delay has to be exactly 1750 ms on an i7-6498DU) will lead to boring and too much localized answers. I have to rely at least in some amount on the creativity and common sense of the contestants. Machines very similar in performance could cause problems as it might be subjective according to which criteria do we hold one to be better than the other. This is why I had to be a little bit vague with "sufficient difference", and I hope the community is creative enough to work with that. (Still, I'm open to suggestions for improving it.) – vsz – 2016-12-06T20:56:44.760

... I just didn't want to fill it with a bunch of excessively strict constraints. – vsz – 2016-12-06T20:57:41.863

5The faster the machine, the slower the program. I don't see an easy way to make this an objective, verifiable criterion – Luis Mendo – 2016-12-06T20:57:47.790

1@LuisMendo: I see at least two ways for easily doing this. – vsz – 2016-12-06T20:58:58.113

@LuisMendo : Did the last edit make it at least a little more understandable to you? There are several ways to easily solve this problem, of course, if won't work if the difference between the performances is too small, as it might depend on may things, like OS version and installed programs. But if the difference between the machines is significant, it shouldn't be a problem. Do you think that the question is impossible to be answered at all, without specifying in exact gigaflops what a significant difference in performance means? – vsz – 2016-12-06T21:13:40.893

1@vsz The problem I see is reproducibility. Someone says they tested on two machines and noticed a significant difference in speed as required, but you can't reproduce that behaviour on your two machines. So is the answer valid? – Luis Mendo – 2016-12-06T21:21:14.260

@LuisMendo : If the method it uses seems reasonable, then yes. (I doubt all accepted answers on all highly rated questions, especially ones in very obscure languages have been verified) Of course, this requires explaining the method they used. I'm adding it to the question. – vsz – 2016-12-06T21:24:22.887

1Should this be tagged with [tag:busy-beaver]? – mbomb007 – 2016-12-06T21:47:24.830

@vsz why not The faster the machine, the slower the program be a bonus? – Mukul Kumar – 2016-12-07T02:27:31.967

2What do you consider to be a “more powerful machine”? Is a machine with the same specs but more RAM considered more powerful? The number of instructions the processor runs in a second? Both? Something else? – Fatalize – 2016-12-07T13:48:38.460

@Fatalize : if I had to define it exactly, I would say a more powerful machine in this context means that a normal implementation of this problem (listing the prime numbers, each in a row), without the tricks of this challenge, would run faster on it. – vsz – 2016-12-07T14:51:45.347

1There's no clear way to quantify that restriction. Voting to close. – mbomb007 – 2016-12-07T20:22:50.760

Answers

4

Perl, 80 78 71 bytes

-9 bytes thanks to @Dada

$_++;`lscpu`=~/z:\s+(\d+)/,sleep$1,(1x$_)!~/^(11+?)\1+$/&&say while$_++

Runs the command lscpu and finds the CPU speed in MHz. The faster the CPU, the more time it sleeps between outputs, 1 second for every 1 MHz. Runs on Ubuntu 14.04.5. On my particular machine, this tests each number every 800 seconds (13 minutes, 20 seconds). On faster machines, this can be over 50 minutes. Change it to sleep$a/400 to get something much more sane for testing purposes.

Gabriel Benamy

Posted 2016-12-06T20:29:47.990

Reputation: 2 827

Rearranging the code a little bit gives $_++;lscpu=~/z:\s+(\d+)/,sleep$1,(1x$_)!~/^(11+?)\1+$/&&say while++$_ for 71 bytes. – Dada – 2016-12-07T17:33:59.750