9 ms·
Qt signal is ten times slower than a virtual function
- skrebbel 4y agoGiven that signals are primarily for interaction with the outside world, (eg user action), 10x slower than the likely optimum seems perfectly fine, no? How many times per microsecond do you expect a user to click that button? I guess what I'm saying is, the example feels contrived.
- windows_sucks 4y ago"onMouseMove" is a user signal that you would want to be fast. also signals should be used for more than just user input interactions, like "download progress" etc..
- ehutch79 4y agoWhat are you doing with onMouseMove that would be perceptible to the user at higher than 60fps, or even 120fps?
- windows_sucks 4y agonothing but if you want to do something, say even at 1fps, you need to enable the signal to fire in the first place.
- dekhn 4y agoI wrote an interactive QtPy program- it receives video frames over the net and allows the user to interact (steering a microscope) in real time. I use millisecond timers (which generate signals delivered to slots) all the time. After doing a bit of tuning I was able to steer the microscope with no visible latency, which means I'm handling user events at ~25FPS or higher and not seeing any high variances. The only problems I have are when the handler that receives a signal takes longer than I have budgeted (IE, more than 1000/25).
- Quikinterp 4y agoDrawing perhaps?
- jonhohle 4y agoDrawing only happens (at most) once every 4ms. I’m not aware of any modern display technology that allows you to manipulate a frame buffer during the display interval (unlike CRTs which could be manipulated during a scan). The retro gaming community is obsessive about input and display latency and even there anywhere between 5-16ms (16ms being one frame of 240p content) is considered acceptable for even the most hardcore twitch response games. That’s not saying that other processes aren’t happening faster than that, just that human input and subsequent visual feedback maxes out somewhere between 200-300 times per second and for the vast majority of humans, it is far, far lower.
- mattkrause 4y agoHumans are even slower than that. If you measure the response of individual photoreceptors, it takes 25-50 ms to peak after a flash of appropriately-colored light; the precise number depends on the color and intensity of the light. After that, the signal still needs to propagate through a bunch of visual brain areas, and then even more needs to happen to somehow influence behavior. With everything tuned just so, you can complete that whole process in 100 ms or so, but the conditions have to be perfect; otherwise, 300+ ms between (simple) stimulus and (simple) response is more typical. Obviously, a lot of this is happening asynchronously, and high refresh rates can help in other ways (e.g., by smoothing out movement), but it astonishing how laggy our visual system is.
- rakoo 4y agoThose random input generators that take every move and feed it to the mixer. Not that they're actually useful.
- thfuran 4y agoOne tenth as fast as a virtual function call is incredibly fast.
- jerf 4y agoI like to bang on the drum that as a programmer, you need to understand the sheer number of orders of magnitude you're spanning more than the average programmer does. We so often deal in "10x slower" and "100x" slower that we can forget that it just doesn't matter if we're doing it even a mere 60 times a second. 10x slower on a process that takes 100ms is a problem. 10x slower on a process that takes 10ns requires a lot of looping to become a human-scale problem. There are things that can attain that level of looping, certainly, but it's not everything. A good programmer ought to have read that sentence and instinctively observed that between 100ms and 10ns is a full seven orders of magnitude. For two numbers that at the human level may seem not terribly far away from "zero", there's a lot of space between them.
- saidinesh5 4y agoI think onMouseMove is a QML/QtQuick specific thing. In C++/QWidgets I remember having to use mouseMoveEvent https://doc.qt.io/qt-5/qwidget.html#mouseMoveEvent https://doc.qt.io/qt-5/qwidget.html#mouseMoveEvent for that. As for download progress etc.. I don't think I have ever had to worry about speed of a function call ever - as long as I was leaving it to the event loop take care of it.
- tlb 4y agoonMouseMove is normally delivered at the video frame rate, 60 fps. The OP's benchmark shows it can deliver around 60M signals per second, so it uses about 1/1000000 of the CPU time. Seems tolerable.
- AlotOfReading 4y agoEven if it was delivered at the polling rate, that should never be higher than 1kHz (otherwise you deserve whatever performance issues you get). A virtual function call is 15ns conservatively, so say a signal is 150ns. 1000x is <150us of wasted time, well below observable overhead in any human-centric application.
- msbarnett 4y agoObjective-C was doing sufficiently-fast UI updates for it to run well on iPhones 10 years ago, while relying on objc_msgsend, which is _much_ slower than a virtual function call or even a Qt signal. You wouldn't want to use ObjC's message sending OR Qt's signalling mechanism in a tight inner loop – hell, you probably don't want to deal with the indirection of the vtable incurred by a virtual function in a tight inner loop. But all of these are more than fast enough for interactive UI work.
- eska 4y agoIn a tight loop you’d resolve the dispatch only once and call the resulting function repeatedly.
- morelisp 4y ago> objc_msgsend, which is _much_ slower than a virtual function call or even a Qt signal. objc_msgsend is slower than a virtual function, but like 1.1x-2x, not 10x. (In rare cases it can even be faster due to it being a little more predictor-friendly.) https://www.mikeash.com/pyblog/friday-qa-2016-04-15-performance-comparisons-of-common-operations-2016-edition.html https://www.mikeash.com/pyblog/friday-qa-2016-04-15-performa...
- ksherlock 4y agoAlso, 30 years ago, on NeXT hardware.
- cma 4y agoMemory latency hasn't improved that much since Next and these kind of virtual-like things cause lots of dependent reads.
- tinus_hn 4y agoObjc_msgsend on the other hand, has been optimized continuously. https://www.mikeash.com/pyblog/objc_msgsends-new-prototype.html https://www.mikeash.com/pyblog/objc_msgsends-new-prototype.h...
- nsonha 4y agonot that fast, it's still an user thing which is incredibly slow comparing to computation
- usefulcat 4y agoThe slowest number mentioned in the post--"32,562,683 signals [per second] with sender"--works out to about 31 nanoseconds. That's around half a dozen orders of magnitude less than an amount of latency that would be noticeable to a human.
- Gibbon1 4y agoThe little 50MHz Arm Cortex I'm using does one tick in 20 ns. Gate delay through old 74LSXX logic was like 10ns.
- slavik81 4y agoIn Qt, those sorts of input interactions are mostly handled through virtual function calls, not signals. You're basically referring to QWidget::mouseMoveEvent. https://doc.qt.io/qt-6/qwidget.html#mouseMoveEvent https://doc.qt.io/qt-6/qwidget.html#mouseMoveEvent
- jollybean 4y agoIf signals were only used for onMouseMove and such things, this wouldn't be big deal. It's when you start to have to use signals thousands of times per event that it becomes a problem. Signals for raw user events won't make a difference, but signals as the main mechanism of API interfacing is a problem. I would say it's a serious problem. I avoid using signals in general.
- sudosysgen 4y agoonMouseMove is going to be called at most like 3000 times a second, assuming a 240hz screen and the signal beinf sent through 12 objects. You're going to be using up, what, 300 microseconds? But even then, you're missing the point, because you don't have to use signals to detect the mouse moving if you don't want to.
- kaba0 4y ago10 times faster than a virtual call is still ridiculously fast on any general purpose CPU made in this century. We often forget how insanely fast they are (and at the same time, I have no idea how we manage to add as much bloat to certain stacks that they make the processor struggle). Like, I’m fairly sure you could have decent latency if your callback function for onMouseMove made a local network call to another process. Also, how fast do you think download progress should update? Animating it to the next keyframe every second is much more than enough.
- xbar 4y ago"Assume I receive 100,000 trades per second from some crypo exchange and if one trade triggers 100 signals than I have 10.000.000 signals per second that is something comparable with the maximum." Computers work faster than people who click manually.
- smiddereens 4y ago
- halter73 4y agoWhy would you use QT signals for that? The point is you do not have to interact with the UI at that high of a frequency.
- dekhn 4y agoThis really sounds like a naive implementation expecting that naive use of a feature is perfectly optimized. IE, don't use that number of signals, or treat signals like a broadcasting message bus. 10X the cost of a virtual function call is pretty optimized, btw.
- skrebbel 4y agoI strongly doubt the signals, even at 10x a virtual function call, are a bottleneck when handling 100k network requests in a second. When you're optimizing that deep, you can do that extra bit of work and replace the qt signal library by a real vtable. It's not what signals were designed for.
- davidw 4y agoI wrote Tcl/Tk code in the 90ies that was perfectly adequate in terms of user interaction, and that is a scripting language...
- morelisp 4y agoI haven't used Qt enough to speak meaningfully about its idioms but in GLib there's a signal for I/O ready on an fd (i.e. effectively a way to plug a select(2) into your UI event loop). I can easily imagine someone, accidentally or ignorantly, using this in a very stupid way.
- inferiorhuman 4y agoThe equivalent is QSocketNotifier (which uses select under the hood) and QFileSystemWatcher (which does complex stuff). It's been a long time since I've spent quality time with Qt but I don't remember anyone complaining about the performance of QSN.
- morelisp 4y agoSure, if you use it right it's not an issue. Not everyone uses everything right, and unlike e.g. mouse move or key press, this one could conceivably fire a million times a second if you use it wrong. Personally, I'm in the "I'm surprised it's only 10x" camp - I wonder who thought signals were fast to begin with?
- inferiorhuman 4y agoMe? The impression I had (granted it's been decades at this point) is that Qt/KDE apps were painfully slow to compile but were usually performant. Look at something like KHTML.
- kaba0 4y agoHow are 10 times a literal virtual call not performant? That’s 150 NANOsec.
- morelisp 4y agoIt sounds like you're not familiar with what signals are used for. I brought up the select(2) example because I wouldn't want to fire a signal for every byte (I probably wouldn't want to call a virtual function call, even). But for clicking the mouse, scrolling a page, kicking off a network request - you could go up to a full ms and you'd still feel that the app is (in a vague general sense) "performant".
- abdulhaq 4y agoFor twenty years no Qt developer has complained about the speed of Qt signals. Even in PyQt the speed of Qt signals is not issue - it's the code responding to the signal that can be a problem. This is a non-issue and anyone who is worried about it is having a severe case of premature optimization.
- antoineMoPa 4y ago> anyone who is worried about it is having a severe case of premature optimization. I think the "This is a premature optimization" mindset is what leads to slow pieces of software that I like to avoid (React, Qt, electron). But I guess it's fine, as most users don't care as much as I do.
- Sakos 4y agoQt is slow now? All the Qt-based applications I've used are as fast as anything else can be.
- simonh 4y agoFrom context it seems poster may believe Qt is some form of web framework, or something.
- antoineMoPa 4y agoNot sure why you believe that, I built some (small) Qt applications so I know it's not a web framework, I was just referring to slow software in general. I think it's less true now, but I remember KDE (Qt-based) being slower and buggier than gnome (Gtk-based) a couple of years ago, just to cite one thing. It just felt like Qt-based stuff was in general more of a pain to use & heavier than GTK based stuff. It's really a matter of personal preference here and Qt is a nice project, just that I have some criticism here regarding performance choices. I feel like bad performance decisions tend to snowball and get multiplied when people make library choices and add their own performance issues on top.
- milliams 4y agonitpick: It's Qt (pronounced "cute"), not QT.
- verall 4y agoI tried pronouncing it as "cute" for a few months before the inertia of everyone I work with calling it "que-tee" won out. I figure if you work on Qt rather than with Qt, saving the syllable is a big win. Descriptivism vs prescriptivism, or something
- mbeex 4y agoMarketing. I used to work with Qt before 2000. In the beginning it was "Quasar Toolkit". Nobody cared and most people I knew - including some of the libraries developers - were fine with kju-tee.
- mlnhd 4y agoMy TA in college said it stood for Quite Terrible. I've used it once since then on an embedded vehicle guidance system, and it seemed fine, once you learn all it's quirks.
- xaedes 4y agoFor me it will always be "cutie". It is feels better to use as noun but is still somehow cute.
- TheCoelacanth 4y agoNot that surprising, because a virtual function call is really fucking fast.
- daoist_shaman 4y agoIf something mission critical is happening in a GUI dependent on Qt, it seems like even the most rudimentary timing analysis of the requirements could have revealed that Qt is not a good solution for that. I fail to see any relevant insights here.
- rat9988 4y agoit seems like even the most rudimentary timing analysis of the requirements could have revealed that Qt is not a good solution for that. How do you deduce this without studying it? Or someone else studying it for you?
- daoist_shaman 4y agoGraphics and graphical processing take orders of magnitude longer than other fundamental machine instructions. It's just inherently slower than doing something equivalent that requires zero graphical processing or monitoring of user input. It is common sense to anyone who knows the tech stack that underlies the pixels on the screen. This is no fault of Qt by any means. It's just the nature of the tech.
- rat9988 4y agoBut he is testing slots and signals, which are not tied to GUI.
- Woodi 4y ago> mission critical is happening in a GUI Example pleas ? That "mission critical" and "in GUI" is kind of strange... But if you just talking about "human reaction is needed" time then 10 x virtual function call time don't looks so bad...
- inferiorhuman 4y agohttps://www.qt.io/industry/qt-in-automotive https://www.qt.io/industry/qt-in-automotive
- formerly_proven 4y agoQt signals are a one-to-many synchronous/asynchronous intra/inter-thread intra/inter-language dynamic/static message passing mechanism, which figures all that stuff out by itself by default. 10x slower than a (presumably hot) virtual function call is actually very good for what you're getting.
- jollybean 4y agoOnly if you need those things, and in most cases, you don't need most of them.
- ragnese 4y agoTen times slower than a C++ virtual function call is still pretty damn fast. Good for them.
- dicroce 4y agoIm actually surprised they are that fast.
- deleted 4y ago[deleted]
- Rochus 4y agoThe claimed 16 times speeddown apparently is caused by a call to sender(); if the object reference is just passed to onValueChangedSignal instead of calling sender() there is likely no speeddown at all.
- saidinesh5 4y agoThe 16 times slow down is when they added call to sender(); But before that their benchmark was for regular signal vs. observer pattern no? ``` void onValueChanged() override { ++m_count; } void onValueChangedSignal() { ++m_count; } ``` QT signal: 00:00:01.841 Virtual function: 00:00:00.179
- colonwqbang 4y agoFrom the Qt manual: >In general, emitting a signal that is connected to some slots, is approximately ten times slower than calling the receivers directly, with non-virtual function calls. https://doc.qt.io/qt-6/signalsandslots.html https://doc.qt.io/qt-6/signalsandslots.html
- 202206241203 4y agoSo, x100 times faster than ReactJS?
- JediPig 4y agoIm never doing another QT contract. I think the last one was my last one. Ill stick to native UI, not electron or anything like that.. The hypocrite in me thinks flutter is my next choice.
- ipython 4y agoOut of curiosity, why?
- NonNefarious 4y agoGreat story, well-supported. I'm going to adopt this philosophy from now on, solely on the strength of your arguments.
- kaba0 4y agoWhat is native UI?
- JustSomeNobody 4y agoQT Signal speed isn't the problem. Focus on making your app fast. Then if that's not enough, make it appear fast. If your report generator takes 5 seconds to generate 400 pages, don't put up a meter to show percentage done. Instead, start showing the user the pages that have already been generated. They won't care that there's 2 more seconds to wait, because they can already start seeing pages.
- spacechild1 4y agoSo what?
- longnguyen 4y agoThe number is about right. I did a benchmark a couple of year ago while developing a realtime 3D frame processing and using Qt signal was about 7-8x slower. But for most of the use-cases like UI interaction handling, the difference is negligible.
- freedomben 4y agoFor events that happen that often, signals are not the right tool/implementation for the job. Deciding on a communication mechanism is a game of balances and trade-offs depending on your needs. HTTP is much slower than some more complex RPC mechanisms, but it's fast enough for most applications. if somebody were blasting enormous amounts of data through it, you wouldn't criticize HTTP as too slow, you'd change to a more appropriate protocol that forces more complexity on the application but is more efficient with time and space. I think you should do the same here and use a different communication mechanism than signals. FWIW, I did some pretty intense real time animation in Qt about 10 years ago, and even on the crap hardware at the time I had no issue using signals to hook into mouse move events (which happen often and quickly sometimes).
- yazzku 4y agoThis has been documented for eons. https://doc.qt.io/qt-5/signalsandslots.html https://doc.qt.io/qt-5/signalsandslots.html "Compared to callbacks, signals and slots are slightly slower because of the increased flexibility they provide, although the difference for real applications is insignificant. In general, emitting a signal that is connected to some slots, is approximately ten times slower than calling the receivers directly, with non-virtual function calls."
- IshKebab 4y ago> Assume I receive 100,000 trades per second from some crypo exchange and if one trade triggers 100 signals than I have 10.000.000 signals per second that is something comparable with the maximum. I dunno how someone is skilled enough to do this benchmark but still thinks that this is how you are supposed to use signals and slots...
- naikrovek 4y agoUgh, what the heck does “ten times slower” mean? “Slow” isn’t a measure, speed is a measure. So, it’s “one tenth as fast”, or “10% as fast”. Even “takes 10 times as long” is good. “Ten times slower” is the only one that doesn’t make sense. English is stupid, but there are rules…
- UncleEntity 4y ago> English is stupid, but there are rules… …and that isn’t one of them. “Twice as slow” is perfectly acceptable, “slow as fuck” is a standard measure of time and “ten times slower” means exactly what it says.
- naikrovek 4y agoTen times slower than what? What is the unit of slow? What is the measure of “slows” that this is ten times greater than?
- T3RMINATED 4y ago