Einführung
Es gibt viele Dienstprogramme, die eine hohe CPU-Auslastung erzeugen können, um Ihre Prozessoren einem Stresstest zu unterziehen. Unter Microsoft Windows können Sie sogar die On-Board-Funktion verwenden calculator.exe
, eine große Zahl wie eingeben 999999999
und n!
mehrmals drücken , damit Ihre CPU (s) länger arbeiten.
Aber was steckt in einer Lösung, wenn Sie sie nicht selbst erstellt haben?
Die Mission
Ihre Mission - wenn Sie sie akzeptieren möchten - ist es, das kleinste CPU-Stresstest-Tool der Welt zu entwickeln.
Muss…
- muss bis zum Abbruch 100% CPU-Last erzeugen
- Es muss eine numerische Eingabe erfolgen, die die Anzahl der Sekunden angibt, die der Stresstest ausführen soll
- müssen Benutzer - Interaktion (keypress, Schließen Terminal - Fenster, oder so ähnlich) ermöglichen , die es einem Benutzer ermöglichen soll , den Stress-Test und / oder beenden Sie das Programm abbrechen
- muss Microsoft Windows, Mac OS X und / oder Linux zum Ziel haben.
(Auch ein Hamster könnte ein Comodore64 betonen ... also Sie müssen ein aktuelles Betriebssystem abzielen.)
Darf nicht…
- Es dürfen keine Programme oder Tools von Drittanbietern verwendet werden, die die erwartete Funktionalität ersetzen.
(Das Vorschlagen von Verknüpfungen wiesystem('cpuStressThing.exe')
disqualifiziert Ihren Vorschlag.)
Kann…
- kann jeden Ansatz / Algorithmus / jede Funktionalität verwenden, um eine erwartete CPU-Auslastung von 100% zu erzielen
- kann jede Programmiersprache oder Skriptsprache verwenden
(sofern dies eine praktische Überprüfung der Funktionalität durch Ausführen ermöglicht)
Gewinnbedingung
Präsentieren Sie den kleinstmöglichen Quellcode. Der Gewinner ist derjenige, der den minimalsten (in der Größe) Quellcode präsentiert, der den obigen Bedingungen „Muss“ und „Muss nicht“ entspricht. Lass das Baby brennen ...
BEARBEITEN
Da die Frage im Kommentarbereich auftauchte, müssen Sie nur 1 CPU-Kern anvisieren. Ich erwarte definitiv nicht, dass Sie eine Multi-Core-Lösung produzieren. Das soll doch Spaß machen - nicht funktionieren.
Antworten:
Bash- und Standarddienstprogramme,
363122292826 Bytequelle
:
indo :; done
. Ich habe herausgefunden, dassdo;done
es die Aufgabe erfüllt - das bringt Sie in 2 Bytes. Außerdem +1, weil es fast halb so lang ist wie meine Bash-Lösung (ich habe es ohne triftigen Grund zu kompliziert gemacht, weil ich es vergessen habe$!
).bash: syntax error near unexpected token `;'
. Ich habe diese bash - Versionen versucht:3.00.15(1)-release (x86_64-redhat-linux-gnu)
,3.2.48(1)-release (x86_64-apple-darwin12)
,4.2.25(1)-release (x86_64-pc-linux-gnu)
ksh
Antwort dann ;-)$1
anstelle von10
dort, nur um es in ein Skript zu machen , dass „nimmt numerische Eingabe“.Bash / iputils (Linux), 14 Byte
Überfluten Sie die IPv6-Nulladresse, bis der Deadline-Timer -w abläuft
Einschränkung - verbraucht nur 55-60% der CPU auf meiner Test-VMBearbeiten: - Ich ziehe meine Einschränkung zurück. Während
top
berichtet, dass derping6
Prozess nur 55-60% der CPU verbraucht, sehe ich, dass der Prozentsatz des CPU-Leerlaufs (2-Kern-VM) sich Null nähert. Dies liegt vermutlich daran, dass ein Großteil der Verarbeitung im Kernel stattfindet, während er die Pakete verarbeitet.Hinweis - muss als root ausgeführt werden. Wie @Tobia kommentiert, scheint dies eine vernünftige Voraussetzung für etwas zu sein, das die CPU belastet. Und das OP hat es in den Kommentaren gebilligt.
quelle
sudo
and just state that the test must be run as root. It seems like a reasonable requirement for something that will hog the CPU.ping -6 :: -t
--> only if this on windows made my cpu go wild... It's just the linux equivalent on windows, which only gives errors and doesn't even load the cpu at 1%! I'm using windows 8 pro x64 on a core2quad 2.63GHz.Elf32 standalone binary - 86 bytes
I bet this is the smallest correctly formed Elf format binary that can be made to perform this function. This will execute without any additional support on any linux based platform, or potentially even without an operating system.
Binary download: http://ge.tt/3m6h2cK1/v/0?c
Hex dump:
This is done by building an asm file with a minimal Elf header of its own, and skipping the use of
ld
altogether.Assembly:
Built with
nasm -f bin tiny_cpu_stresser_elf32.asm -o tiny_cpu_stresser_elf32
quelle
bash builtins only 20 bytes
quelle
C, 52
Press Ctrl+C to exit.
quelle
$i=<>
part and add<?
to the beginning and you are good to go!Perl, 32
Now the embarrassing part: I foolishly put
$t=time
in front of$i=<>
and was furiously trying to figure out why it exits a few seconds early.Again, Ctrl+C to exit.
quelle
Unix C, 47
Pass the time on the command line. Interrupt key (Ctrl-C) aborts.
quelle
scanf
trick, you can get this down to 39:main(a){for(scanf("%d",&a),alarm(a);;);}
Smalltalk (Smalltalk/X), 34
input: n; interrupt with CTRL-c or CMD-.
can golf better, if measured in days ;-) (just kidding):
or from a command line:
quelle
This is not a serious attempt at it, but...
Bash, 12 bytes
As found on Wikipedia.
WARNING: harmful code, don't run it on your computer!
Technically:
- It produces 100% CPU load until system crashes;
- Allows user interaction to stop it (if you manage to kill all the forks, you can actually stop it...);
- You could give it a numeric input that represents the number of seconds it should run, but it won't use it.
quelle
PHP
4340 bytes:I hope this is an acceptable answer:
set_time_limit($_REQUEST['t']);while(!0);I could do like this:
<?for(set_time_limit($_POST['t']);;);
but it would lose flexibility and 3 bytes.And i could cheat and do like this:
<?for(set_time_limit($_REQUEST[t]);;);
. It shaves off 2 bytes, but it's not a "standard" solution. Lets keep the game fair.As @fireeyedboy and @primo suggested, you can also use this solution (34 bytes):
This allows it's use from the console, calling it like this:
As i told, I'm not targeting the console solution, but they have to get the credit for this one.
Another answer could be this "monster", which is just both answers combined:
It's impossible to get key presses in php, without being on console, which I'm not targeting!
To stop it, you MUST abort the process (stopping the page from loading might stop the code)!
As a plus, it works in Android too! If you install a php server (free on Google Play).
To make it work, simply do like this:
You create a .php webpage and append
?t=<time in seconds>
to the end of the url or submit a post (using a form or even ajax).quelle
$_REQUEST['t']
with$argv[1]
then call it with:php -f cpustresstest.php <timelimit>
and abort with^C
.$_GET
instead of$_REQUEST
? 4 bytes and you are usingGET
anywayBrainFuck/Extended BrainFuck: 3
It will use 100% cpu on one core until aborted. All Brainfuck programs are valid EBF programs.
Zozotez LISP: 7
1519When using the little driver.
As a standalone expression without a driver: 15
Usage:
echo '((\(L)(L))(\()(L)))' | jitbf zozotez.bf
quelle
Perl - 14 bytes
Sets a
SIGALRM
to be sent ininput
seconds, which terminates the script. In the meantime, it spins in a busy-wait.Sample usage:
Perl - 12 (+1) bytes
If command line options are counted as one byte each, this could be reduced to 13 bytes using a
-n
:Sample usage:
quelle
x86_64 assembly on Linux - 146 (source), 42 (assembled code)
The NASM minified source (146 bytes):
Accepts a parameter on the command line specifying the number of seconds to run in the range (0, 9999999]; can be interrupted with the usual Ctrl-C.
You can assemble it with
In theory it would be necessary to add a
global _start
followed by a_start:
label at the beginning, butld
manages to fix it by itself with little fuss.The corresponding machine code (42 bytes):
(generated with
nasm
adding theBITS 64
directive)A somewhat more readable version:
quelle
Python,
585551Wow... longer than the C one. There's got to be a better way.Still a tad long, but at least it beats the C solution!quelle
Java -
154 148186Weird error ate my
Thread.sleep()
partand a more readable version:
Spawns a
new Thread
with a nice endless loop (for(;;);
) then on main thread athread.sleep()
and aSystem.exit(0)
after timeout to exit; ctrl-c exits, too on cmdline wasnt able to shorthand thatexit()
. crashing wont work;quelle
Batch, 2 characters
%0
In essence, the program constantly starts itself over and over. Your results may vary, due to processor task allocation priority, but it works for me.
quelle
Powershell,
185450 bytesTo produce 100% load for all CPU cores.
| AddS*
is the shortcut for.AddSeconds()
method.sajb
is the alias forStart-Job
cmdlet.quelle
must take a numeric input, representing the number seconds the stress-test should run
)?Linux sh and standard utilities, 14
Recent gnu coreutils includes a
timeout
utility which is helpful:quelle
timeout $1 yes :|sh
- 19 is probably the best you can do and get 100% utilisation. Tempted to steal this for my answer, but I'll be sportsman-like :)Matlab - 19
tic;while toc<5;end
Replace
5
with desired execution time.quelle
Go,
215212193 bytes (full)Bonus, stresses all CPU's.
The
Now()
in the loop is there to kick in the scheduler,Now
was the shortest function name I could find in my namespaceIf I run
go fmt
the size increases to286277254 bytesquelle
Bash: 19 chars
quelle
Assembly: 16 bytes
Edit: Having not noticed the requirement to take a numeric input, i'm going to claim it does take one on the commandline, but ignores it =)
quelle
DOS Batch - 5 bytes
DOS Batch - 8 bytes
Second is a translation of the infamous sh forkbomb.
Ctrl+C breaks the program (unless you've tweaked the settings a little).
quelle
C#, 118
Uncompressed
This requires a number as an argument which is the number of seconds to run. It will use 100% of one core for that much time or until crtl+c. I'm pretty sure this is as small as C# will go with its verbosity.
quelle
C# - 178 characters
And more readable:
Thats 178 chars in C# and uses all cores.
The only weakness that it is always ending because of the 1<<30 integer limit.
quelle
Java - 88 characters
This allows for 2⁶³-1 loops.
More Readable Version
C# - 87 characters
More Readable Version
(This is on a 4 core system)
quelle
EcmaScript 6:
This will use 100% of the CPU on a single-core machine, and with Firefox, it has the added bonus that Firefox keeps using up more and more memory; the whole interface locks up and the only way to stop it is to kill Firefox in the task manager.
quelle
perl, 23 bytes
I can't figure out how to paste a literal control-T here, so I've typed $^T instead, but either works (the literal is 1 char shorter at 23 bytes):
$^T is just the time the interpreter started, so you can basically read that as time() since it is the first thing we calculate.
quelle
Python, 30
I found this old puzzle interesting, I hope it's OK to post an answer to an old question. I just couldn't let the C answers beat Python. ;)
sum(range(int(input())*2**26))
This needs tuned for different CPUs, but I don't think that violates the OP...
sum(range(2**27))
pegs one of my 2.8GHz i7 cores for about a second. :)quelle
must produce 100% CPU load until aborted
.must take a numeric input, representing the number seconds the stress-test should run
. So that the code needs to take user input somehow and self-limit accordingly, as well as just peg a CPU. This is the part I found most interesting about the puzzle...