22 ms·
How much your computer can do in a second
- dom0 10y agoMore impressively, sum.c could go likely an order of magnitude or so faster, when optimized. > Friends who do high performance networking say it's possible to get network roundtrips of 250ns (!!!), Well stuff like Infiniband is less network, and more similar to a bus (e.g. RDMA, atomic ops like fetch-and-add or CAS). > write_to_memory.py Is also interesting because this is dominated by inefficiencies in the API and implementation and not actually limited by the memory subsystem. > msgpack_parse.py Again, a large chunk goes into inefficiencies, not so much the actual work. This is a common pattern in highly abstracted software. msgpack-c mostly works at >200 MB/s or so (obviously a lot faster if you have lots of RAWs or STRs and little structure). Funnily enough, if you link against it and traverse stuff, then a lot of time is spent doing traversals, and not the actual unpacking (in some analysis I've seen a ~1/3 - 2/3 split). So the cost of abstraction also bites here. If you toy around with ZeroMQ you can see that you'll be able to send around 3 million msg/s between threads (PUSH/PULL) from C or C++, around 300k using pyzmq (this factor 10 is sometimes called "interpreter tax"), but only around 7000 or so if you try to send Python objects using send_pyobj (which uses Pickle). That's a factor 430.
- p1esk 10y agoHow would you optimize sum.c to be faster?
- gravypod 10y agoGCC with -O3 might try to unroll this loop into a single constant. O(0) is pretty fast.
- supergarfield 10y agoThat's very likely. Clang 8.0 collapses the loop with any optimizer setting other than -O0. But if you had to do it manually, loop unrolling and SIMD instructions (although not part of standard C) would be good bets and can probably get you an order of magnitude.
- dom0 10y agoGCC as expected, O>2 sub rsp, 8 mov rdi, QWORD PTR [rsi+8] mov edx, 10 xor esi, esi call strtol <-- atoi xor eax, eax <-- zero return value add rsp, 8 ret <-- return from main Interestingly enough it does not elide the entire function body.
- gravypod 10y agoHow do you get objdump -S to print out non AT&T syntax? My output looked like this: 000000000400400 <main>: 400400: 48 83 ec 08 sub $0x8,%rsp 400404: 48 8b 7e 08 mov 0x8(%rsi),%rdi 400408: ba 0a 00 00 00 mov $0xa,%edx 40040d: 31 f6 xor %esi,%esi 40040f: e8 dc ff ff ff callq 4003f0 <strtol@plt> 400414: 31 c0 xor %eax,%eax 400416: 48 83 c4 08 add $0x8,%rsp 40041a: c3 retq 40041b: 0f 1f 44 00 00 nopl 0x0(%rax,%rax,1) Which I'm not a fan of. I've always wanted "add (store), (get)" but could never get that.
- dom0 10y agoNot on a development machine right now, so I just went to godbolt ;) https://godbolt.org/ https://godbolt.org/
- userbinator 10y agoUse the '-M intel' option.
- gens 10y ago-M intel
- deleted 10y ago[deleted]
- jwilk 10y agoITYM "O(1)". "O(0)" is pretty much meaningless.
- gravypod 10y agoI mean O(0) because no operation occured. The only operation that will occur is parsing the data which is not the problem specified. When looking at the loop, and just the loop, O(0) is the correct answer for this.
- dom0 10y agoWell the obvious answer would be, eliminate the loop, which is what any compiler optimizer will do ;) But let's assume that the operation is not quite so trivial like here, then this structure would be a prime example where each loop operation is independent from each other, so you can sum a vector in parallel (=SIMD) and then sum the vector once in the end. Also, since we're obviously not using any compiler optimizations, you can unroll the loop, which reduces per-iteration loop overhead (the conditional jump) and in such a simple case as here is bound to give a nice boost.
- p1esk 10y agoWhen you say "eliminate the loop", do you mean loop unrolling? They are compiling with -O2, I'm not sure if that does loop unrolling. If it does, how much unrolling does it do, exactly? I can't imagine it would construct a block of code with a billion add instructions.
- corndoge 10y agos += NUMBER or s = NUMBER
- danellis 10y agoEliminating the loop means the compiler looks at the code and says "the value computed in this loop is never actually used, so might as well just get rid of that code altogether".
- DannyBee 10y agoFirst, it's unused, so it'll just kill it. If it wasn't, most compilers can compute sum reductions, etc, pretty well. Anything that is easily expressed as a set of simple recurrences that can be solved, it'll solve.
- halomru 10y agoYou can't really do loop unrolling and constant folding as some suggest because the number of iterations is determined at run time. But assuming his CPU takes one cycle for an addition and one cycle for a conditional jump, his CPU only needs to run at 1GHz to achieve his result. Given that branch prediction should be nearly perfect for such a simple and short loop, modern x86 CPUs doing at least 4 integer additions per cycle, and typical CPU speeds in the range of 2-4 GHz, a properly optimized version should be nearly an order of magnitude faster. So either more aggressive compiler flags and maybe SIMD intrinsics, or hand written assembly (easy here, not so easy in the real world)
- dom0 10y ago> You can't really do loop unrolling and constant folding as some suggest because the number of iterations is determined at run time. Huh?
- halomru 10y agoThat question is a bit unspecific :D >NUMBER = atoi(argv[1]); argv is populated at runtime based on what the OS passed you. But you are right if you meant that my statement is too strong: you can't do arbitrary loop unrolling. You can jump to a loop that's unrolled once if the number is even, etc. There are certainly also optimizers that will just output the input without any loop, but that's beyond anything I would expect of a current production compiler.
- gravypod 10y agoIf you are going from 0 -> NUMBER and adding one to a variable every iteration, what will the variable's final result be? NUMBER. The compiler can see this and will just do the following: s = i = atoi(argv[1]); Now the loop is gone. Edit: This will still happen even if you're doing other stuff in there. GCC will attempt to inline and optimize like this. If it is impossible then you will see a loop with most of the stuff factored out of it.
- EvilTerran 10y ago
- DesiLurker 10y agofor a data parallel problem like that you could vectorize it using the SIMD unit. It never ceases to amaze me at any point how much of silicon real estate just sits idle because no one really bothered to look behind the curtain and tailor the code for a specific architecture. for reference, a recent Intel chip will have 256b wide vector unit (512 for server class chips) that can be treated as a vector of 8/16/32/64b units to do parallel math. at a typical 16b ops you get 16x speedup for practically nothing. plus its considerably more if you have predictable but non standard data access patterns. now <rant> IMO what the parent commenter was trying to say is often this punchline about premature optimizations used as catch-all justification for all the convenience/expediency related decisions taken. & also it just works well to create future work. unfortunately that never gets scheduled as I have seen management time frame shrink too. almost everyone is in quick & dirty 'working' version to book a success and move on. with this culture if you try to push resolving these 'technical debt' type issues you are just perceived as a downer. </rant>
- p1esk 10y agoI've heard that modern processors are designed with the expectation (or even a requirement) that no more than 10% of the chip will be active at once. In addition, there are power density constraints (even those 10% of the chip that can be active at once must not be concentrated in the same spot).
- dom0 10y agoNope, but processors, especially larger ones, have pretty advanced power and clock management which allows them to deal with power envelope and thermal envelope constraints. E.g. the two clocking and core voltage ladders in Xeon E5/E7 for AVX vs. non-AVX code. (Though this particular example is a bit of a sledge-hammer approach).
- deleted 10y ago[deleted]
- gizmo 10y agoPretty cool, but a number of the questions are totally unknowable. For instance the question about web requests to google. Depending on your internet connection you've got more than a order of magnitude difference in the outcome. In the question about SSD performance the only hint we have is that the computer has "an SSD", but a modern PCIe SSD like in the new Macbook pro is over 10 times faster than the SSDs we got just 5 years ago. The question about JSON/Msgpack parsing is just about the implementation. Is the python msgpack library a pure python library or is the work of the entire unpackb() call done in C? The bcrypt question depends entirely on the number of rounds. The default happens to be 12. Had the default been 4 the answer would have been 1000 hashes a second instead of 3. Is the python md5 library written in C? If so, the program is indistinguishable from piping data to md5sum from bash. Otherwise it's going to be at least an order of magnitude slower. So I liked these exercises, but I liked the C questions best because there you can look at the code and figure out how much work the CPU/Disk is doing. Questions that can be reduced to "what language is this python library written in" aren't as insightful.
- dom0 10y agoYou very well captured the intention of this (or at least what I think is the intention). I mentioned above that in highly abstracted systems it is not at all unusual to spend more time with inefficiencies rather than doing real work: You noticed here that it is also hard to tell in a high abstraction layer which operations might be fast and which may be slow. Often this is even found in seemingly simple places, e.g. traversing a List<> in Java can have vastly different performance characteristics (LinkedList vs. ArrayList).
- dragontamer 10y agoThis is exactly my main complaint about this. Still, it was an intriguing idea, even if a lot of the answers were based on things not described. I did learn a few things... I don't do much parsing so I didn't realize how slow JSON was compared to other packaged formats.
- geogriffin 10y agoit's the required escaping of quotation marks!! if you have non-english users and didn't turn off escaping of non-ascii then you're in for a real surprise :) length-prefixed strings would solve this. also, memory allocation penalties can hit you hard, depending on your environment, since it's hard to predict what you'll need to allocate when encoding / decoding json
- userbinator 10y agoAlternatively, this could be titled "do you know how much your computer could do in a second but isn't because of bad design choices, overengineered bloated systems, and dogmatic adherence to the 'premature optimisation' myth?" Computers are fast, but not if all that speed is wasted. A recent related article: https://news.ycombinator.com/item?id=13940014 https://news.ycombinator.com/item?id=13940014
- agumonkey 10y agoThe market is wasteful. Lots of things are subpar because of the competition requiring adhoc solutions pushed to market, then becoming standard, so on and so forth. You can blame the game or not. At least if there was some acknowledgement of that process and a little cleanup time to spread good ideas and good fix ... Personally I always feel weird booting up old boxes (say old = Pentium 2) and realizing how much the whole platform could do. It also felt quite sufficient in terms of UX and presentation. That's odd because, everybody (I assume) feels how quickly we feel our newest gear gets slow, and how a new generation improves speed. But when I pop a very old system I don't feel slowed down much. It's just of a lesser resolution (hi res images, sound, videos); which for my needs doesn't really matter.
- gizmo 10y agoAn eye-opening experience is running an IDE like Visual Studio 6 or a really old version of Photoshop on a modern machine. It starts instantly, compile times are a fraction of what we're used to. The interface is totally responsive. It's remarkable how much better software feels when everything is instant.
- userbinator 10y agoDeveloper tools like Visual Studio are somewhat ironic in that the developers who create inefficient software are essentially seeing this reflected indirectly in their tools also becoming inefficient. A perverse version of "do unto others as you would have them do unto you"?
- d--b 10y agoor "computers are fast, so we might just slow things down by using python for numerical calculations"
- asrp 10y agoThis reminds me of "Latency Numbers Every Programmer Should Know" https://gist.github.com/jboner/2841832 https://gist.github.com/jboner/2841832 Edit: Just realized halfway through that there's already a link to this from their page!
- gibsjose 10y agoI'm curious to see the data collected on guesses. Some were quite difficult to guess, like hashes per second with bcrypt not knowing the cost factor, but I guess we can assume some sane default. I would have really liked to see all these numbers in C, and other languages for that matter. Perhaps add a dropdown box to select the language from a handful of options?
- realo 10y agoYes, modern computers are fast. How fast? The speed of light is about 300,000 km/s. That translates to roughly 1 ns per foot (yeah, I mix up my units... I'm Canadian...) THUS, a computer with a clock speed of 2 GHz will be able to execute, on a single core/thread, about 4 (four !) single-clock instructions between the moment photons leave your screen, and the moment they arrive into your eye 2 feet (roughly) later. _That_ should give you an idea of how fast modern computers really are. And I _still_ wait quite a bit when starting up Microsoft Word.
- digi_owl 10y agoThe thing is that while CPUs are fast, RAM, never mind a HDD, are not.
- dahart 10y agoHoly crap, you're right. 2 instructions per light-foot! That is a very cool comparison! Makes me think maybe light is slower than I thought. And that Word must have a lot of code running. :P
- benchaney 10y agoMost modern CPU's can execute more than one instruction per clock cycle on a single core or hardware thread. They do a lot more than 4 instructions in the time light can travel 2 feet.
- dom0 10y agoAlso: While SIMD does not execute additional instructions, it executes more operations [that's the whole point; it reduces contention and bandwidth needs in the frontend / instruction decoding and scheduling]. A Haswell core peaks at 32 SP FLOP/cycle, for example.
- tachyonbeam 10y agoIf you want to get technical, modern CPUs can dispatch (begin the execution) of 4 to 6 instructions every clock cycle. However, many instructions take more than one cycle to complete. For instance, integer multiply takes 3-4 cycles on an Intel Sandy Bridge chip. FMUL takes 5 cycles to complete. That being said, there are several instructions, like mov, integer additions and compare, bitwise operations, which can absolutely complete in a single cycle. See this document for more details: http://www.agner.org/optimize/instruction_tables.pdf http://www.agner.org/optimize/instruction_tables.pdf
- alkonaut 10y agoDon't some of these examples run in O(1) time because the value in the loop isn't used? E.g in the first example 0 is returned instead of the sum. Obviously we are talking about real world c compilers with real world optimizations so presumably we'd have to also consider whether the loop is executed at all?
- Lxr 10y agoWhy isn't the first Python loop (that does nothing but pass) optimised away completely?
- JoshTriplett 10y agoBecause the Python interpreter doesn't do that optimization. (PyPy might, I don't know.)
- dom0 10y agoCPython only does very few optimizations on the byte code. Most optimizations are in the interpreter itself.
- adrianratnapala 10y agoCompilers often don't optimise loops away because they feel that the user put them there for a reason -- after all they are so obvious that the user could have removed them herself. One use for such a loop is a delay -- not so common nowadays, but it used to be a mainstay of DOS based games etc. I bet that if GCC started aggressively optimising out empty loops, it would interact with some subtlety of concurrency to break a spinlock or three in various kernels.
- danellis 10y agoCompilers can only optimize away loops if they know that the loop has no side effects. Compilers like GCC have to have a list of standard functions that they know are pure. This can trip you up sometimes. For example, if you do something like use memset to zero out sensitive data when you're done using it, the computer can say "the result of memset is never used, and the reference to that memory is lost, so there's no need to actually make the call" and you're left with secrets in memory.
- saagarjha 10y agoForgive me if I don't know what I'm talking about here, but could volatile be used here to force the compiler to perform the memset?
- alcuadrado 10y agoThis reminds me to this email from LuaJIT's list: Computers are fast, or, a moment of appreciation for LuaJIT https://groups.google.com/forum/#!msg/snabb-devel/otVxZOj9dLA/rgCojUohBGMJ https://groups.google.com/forum/#!msg/snabb-devel/otVxZOj9dL...
- munificent 10y agoIf, like me, you spend most of your time in high-level, garbage collected "scripting" languages, it's really worth spending a little time writing a few simple C applications from scratch. It is astonishing how fast a computer is without the overhead most modern languages bring in. That overhead adds tons of value, certainly. I still use higher level languages most of the time. But it's useful to have a sense of how fast you could make some computation go if you really needed to.
- blacksmythe 10y ago>> It is astonishing how fast a computer is without the overhead most modern languages bring in. The other interesting thing with high level languages (using Python as an example) is that a small number of features that are not often used keep the language from running significantly faster (see PyPy for example). Seems like there should be room for a language with slightly reduced set of Python features (Py--) which runs much faster.
- TeMPOraL 10y agoWorth checking out Common Lisp. It's as high-level language as you can get, and yet good compilers (like SBCL, or like commercial ones from Franz and LispWorks) can compile it to tight assembly with performance very close to that of C++ (you need to disable some runtime checks for that though, but you can do that on a per-function level, so it's much less of a problem than one thinks).
- runeks 10y agoHaskell is also really fast. On most Stack Overflow questions asking "why is this Haskell program slow", there's often a detailed answer with an implemention that's about as fast as -- or even faster than -- C: http://stackoverflow.com/questions/42771348/why-is-haskell-so-slow-compared-to-c-for-fibonacci-sequence http://stackoverflow.com/questions/42771348/why-is-haskell-s... http://stackoverflow.com/questions/6964392/speed-comparison-with-project-euler-c-vs-python-vs-erlang-vs-haskell http://stackoverflow.com/questions/6964392/speed-comparison-... http://stackoverflow.com/questions/29875886/haskell-why-does-int-performs-worse-than-word64-and-why-my-program-is-far-slow http://stackoverflow.com/questions/29875886/haskell-why-does...
- gburt 10y agoThe `bcrypt` question seems out-of-place. It has a configurable cost parameter, so almost any of the answers is correct.
- thomastjeffery 10y agoOr "how fast can one of my 8 CPU cores run a for loop?" To put that in perspective: all 8 cores together give me about 40gflops. I have 2 GPUs that each give me more than 5000gflops.
- joelthelion 10y agoThis could make a pretty good hiring test. Not expecting perfect answers, but a rough correlation with the results, and some good explanations.
- Eliezer 10y agoWhat an excellent teaching pattern - you're far more likely to remember what you learned if you first stop to think and record your own guess, and this is excellent UI and UX for doing that routinely and inline.
- panic 10y agoThis page from the New York Times is another great example of teaching through interaction: https://www.nytimes.com/interactive/2017/01/15/us/politics/you-draw-obama-legacy.html https://www.nytimes.com/interactive/2017/01/15/us/politics/y...
- paulsutter 10y agoThat's nothing. Here's code that does 77GFLOPS on a single Broadwell x86 core. Yes that 77 billion opertaions per second. http://pastebin.com/hPayhGXP http://pastebin.com/hPayhGXP
- voltagex_ 10y agoInteresting that that code looks more like Assembly than C to me.
- grepthisab 10y agoEdit: I'm an idiot
- chacham15 10y agoBe careful what conclusions you attempt to draw from examples when you arent sure what exactly is happening. These examples are actually very wrong and misleading. Take for example, the first code snippet about how many loops you can run in 1 second. The OP fails to realize that since the loop isnt producing anything which gets actually used, the compiler is free to optimize it out. You can see that thats exactly what it does here: https://godbolt.org/g/NWa5yZ https://godbolt.org/g/NWa5yZ All it does is call strtol and then exits. It isnt even running a loop.
- justonepost 10y agoSmart computer can do dumb things very very very fast.
- gizmo 10y agoThe page claims the C program can do 550 million loop iterations in one second. That's exactly what you would expect from an average laptop. So the loop clearly isn't getting optimized out in the executable used by the authors. If you compile with -O0 you can confirm this for yourself. You can also see that in later examples printf() statements are added to stymie the optimizer.
- andreyf 10y agoSeems clear the question assumes you're generating code that actually increments the integer (i.e. repeating a single add instruction). An anecdote defending the question on a non-technical level: when I was a kid around 4 or 5 my dad had me race a computer counting to 1000. It was one of the more memorable parts of my young childhood!
- deleted 10y ago[deleted]
- deleted 10y ago[deleted]
- Klockan 10y ago> You can see that thats exactly what it does here: https://godbolt.org/g/NWa5yZ https://godbolt.org/g/NWa5yZ You are wrong as well, that is what it is doing if you compile it with -O2 on that exact compiler. Try compiling with -O0 instead and it will include those instructions. Also the number is reasonable, modern cpu cores can execute around a billion loop iterations per second.
- partycoder 10y agoComputers are fast unless your algorithm is quadratic or worse, then there's no computer to help you.
- bch 10y agoHard to believe there are 124 comments here and nobody has brought up Grace Hopper's talk[0][1] yet. With good humour she gives a example of what various devices' latency are, and a simple tool to comprehend the cost and orders of magnitude. [0] short - https://www.youtube.com/watch?v=JEpsKnWZrJ8 [1] long - https://www.youtube.com/watch?v=ZR0ujwlvbkQ
- joshkpeterson 10y agoCheck out this awesome zine about networking the author made. IN COLOR! http://jvns.ca/zines/#networking-ack http://jvns.ca/zines/#networking-ack
- bane 10y agoThis is awesome. The real lesson here is, when you make a thing, compare its performance to these kinds of expected numbers and if you're not within the same order of magnitude speedwise, you've probably screwed up somewhere. My favorite writeups are the ones that gloat about achieving hundreds of pages served per second per server. That's terrible, and nobody today even understands that.
- kobeya 10y agoWas disappointed to find that nearly all the examples were Python and shell script. I'm not interested in knowing random trivia about how slow various interpreters are.
- tomc1985 10y agoOne second on what? A Core i7? A raspberry Pi? A weird octo-core dual-speed ODROID? An old i915-based Celeron? My cell phone? An arduino? "Your computer" has meant all the above to me, just in the last few weeks. The author's disinclination to describe the kind of hardware this code is running on -- other than "a new laptop" -- strikes me as kind of odd.
- wtbob 10y agoWell, my computer won't display an image apparently inserted with JavaScript, although it could if I wanted to grant execute privileges on it to computers-are-fast.github.io Does anyone have a link to the image(s)?
- deleted 10y ago[deleted]
- sriku 10y agoThe grep example should search for one character. Grep can skip bytes so that longer search strings are faster to search for. On my machine, I get from 22%-35% more time taken if I changed "grep blah" to "grep b".
- ge96 10y agoI came across this "article"? before in the past, I feel like I remember it under a different title like "language speed differences" or something. Or maybe that's another article by the same author/site/format.
- norswap 10y agoBrilliant! I'd like to see those numbers summarized somewhere though, a bit like the latency numbers every programmer should know: https://gist.github.com/jboner/2841832 https://gist.github.com/jboner/2841832 (visual: https://i.imgur.com/k0t1e.png https://i.imgur.com/k0t1e.png)
- urza 10y agoAnyone care to rewrite these into c#? I am really surprised how fast these python scripts are and I would like to see comparison with equivalent tasks in c# where it stands..
- tim333 10y agoOr running Windows Vista you can right click and display a menu in one second plus about 29 other seconds.
- brianwawok 10y ago> GitHub Pages is temporarily down for maintenance. Ironic?
- ilaksh 10y agoNVMe SSD can be up to 10X faster than SATA.
- samirm 10y agoThe last question seems really misleading. Most modern CPUs have a cache size of 8MB (max), yet the answer is >300MB?