3 ms·
As someone who doesn't know Lua and after reading this article, can it properly be said that a scripting language is fast if you write most of your time sensiti
by labrador 10y ago
As someone who doesn't know Lua and after reading this article, can it properly be said that a scripting language is fast if you write most of your time sensitive code in C? Why not just C?
- williamaadams 10y agoIt really depends on your use case. I write all my tight loop code in LuaJIT itself, rather than calling out to C. Lots of people use LuaJIT as the glue to highly optimized long lived and proven C code libraries. The case for doing the glue in Lua is the fact that script is arguably easier to prototype in. So, depends on your particular application space. C can be a lot more error prone, at least when it comes to memory management, but this is a general argument for garbage collected vs not.
- rogerbinns 10y agoI do python where your question applies too. It is significantly quicker to write code in a higher level language, since the primitives and libraries do more per line of code. The cost is that it doesn't run as fast. But consider these two scenarios: 1 It takes 2 hours to write the code, and 1 hour for it to run. 2 It takes 2 days to write the code, and 1 minute for it to run Note that "write the code" includes the actual writing, as well as debugging, testing etc. The first scenario is the most immediately productive. But the more you have to run the code, the more the second scenario applies. Which is why you combine the approaches. Write the parts that aren't performance sensitive quickly in your high level language, and take longer on the performance parts in C. That works better than exclusively approach 1 or 2.
- labrador 10y agoThanks, I think you make a good case for using a language like Lua or Python with an excellent C interface. In some sense, writing everything in C could be a case of premature optimization.
- rogerbinns 10y agoAnother benefit of Lua/Python is that it is considerably easier to write testing and debugging versus C. They support introspection and similar techniques. A very typical example is something I did a while back. I wrote the processing in Python first. The core was 10 lines of code and took about 5 minutes to run on a unit of data. It took about an hour to write, and was correct. It then took two days to rewrite in C and was 400 lines of code, executing in a second or so. Much of the time was finding and fixing minor bugs. But having the "correct" Python code made it very easy to feed the same test data to both implementations and verify the output. Writing test code in C would have taken even longer than just the algorithm!