Reputation: 798
I currently use QtScript for scripting functionality in my C++ application, but it's rather "heavy" on the cpu. When a thread evaluates all the scripts in a loop the cpu usage increases to 90%-100%. Even when i put it to sleep for 1 msec every 5 scripts it stays above 75% cpu usage.
Are there any other, easy to implement, scripting frameworks which are much more lighter than QScript?
edit:
I now realize this is normal behavior and not some hogging bug in QtScript. Still it's interesting to hear what kinds of (lighweight) scripting libraries are available.
Upvotes: 3
Views: 3711
Reputation: 1
Nothing wrong with QtScript from what I can tell so far (only started using it from 4.6 so still new to it, but liking it so far). Depends on how you use it, just like lua or python. If you keep as much of your application's core functionality native (compiled from c/c++) and only expose a minimal API to the script engine then, in general, you can keep things snappy.
With QtScript it is relatively easy to expose objects and their methods in a reasonably thread-safe manner (QT's slots and signals object model) and easily pass script created objects to native functions... but you can end up being tightly integrated with the QT environment (Which might be exactly what you want sometimes).
If you want to arbitrarily expose native c++ objects to other embedded scripting environments checkout SWIG. SWIG is similar to the toLua tool, but works for many embeddable languages like lua, c#, tcl, java and python to name a few. From experience, toLua made binding objects and methods to scripts a much less painful process, and I haven't heard many bad things about SWIG either.
Upvotes: 0
Reputation: 5787
You can also embedd javascript with spidermonkey I think javascript is more widespread than lua.
Upvotes: 0
Reputation: 2247
It really depends on several factors:
I also recommend Lua, however, you should keep the following in mind: Lua is implemented in pure ANSI C. This makes it uber-portable, but if you're developing in a C++ environment, it will lead to a lot of "wrapping" classes. Especially when you would like to expose Qt functionality (with all it's SIGNAL
s, SLOT
s, and PROPERTY
s), it leads to a lot of duplicate code.
Upvotes: 1
Reputation: 41393
Lua is your language of choice. There are some bindings for Qt.
Upvotes: 1
Reputation: 4762
I personally would recommend Lua having used it extensively in our embedded platform. If you are running on windows, you might be able to use something like LuaJIT to make your Lua even faster
However, since no one has mentioned it, you might also want to take a look at Squirrel (http://squirrel-lang.org/). I haven't had any experience with it, but I think it looks promising.
As to your currently problem, any code will take 100% of the CPU if it doesn't have anything that will block it.
something like (pseudocode):
for(i=1,10000000000000) n=n+i end
will take 100% of the CPU until it completes in (almost) any language because there is nothing to stop it from executing.
Upvotes: 2
Reputation: 41096
Well, what do you expect? Unless the script has to wait for disk or user I/O, the CPU should run at 100%.
Is your problem that it runs to long?
Or that your application is unresponsive?
In that case, the problem is that your script is blocking the thread where all the UI interactions run. The general solution is to block all UI input (except the "cancel script" button :)), and move the actual processing to a separate thread.
[edit]
Slightly different question: is the CPU at 100% while there is no script to process?
100% CPU is good and healthy, if you are processing something.
The CPU is always busy, the current thread always consumes 100% of the core it is running on. "0% CPU activity" actually means all cycles are spent in an system idle thread (that belongs to the "system idle process" you see in task manager).
As an simplistic example: if you have one application thread active, and CPU usage is 40%, and your task manager update interval is 1s, 400ms of CPU time was spent on the application, and 600ms on the idle thread.
Upvotes: 12
Reputation: 8727
I've heard good things about TinyScheme. That having been said, we're using Lua here (at a game development studio, targeting embedded and hand-held systems).
Things to note, though -- with Lua specifically, but I think these apply to many of these languages:
Upvotes: 2
Reputation:
Lua is good because it uses a stack to communicate between interpreter and C++. This is good, because it doesn't involve any reference counting visible to you which simplifies things.
Here is an interesting comparsion as a background for some iolanguage: iolanguage.
Upvotes: 3
Reputation: 33318
Have a look at Lua, it's frequently used in games so the performance must be pretty good.
Upvotes: 16