Lua Vs C++ Speed

  1. Lua Vs C++ Speed Test
  2. Lua Vs C++ Speed Download
  3. Lua Vs C++ Speed Calculator

Should I learn Lua or C?C is used almost everywhere.It is getting supplanted by newer languages, even Objective C.Objective C is a version of C.

ESP8266 frequency generator


I started an ESP8266 square wave frequency doubler project for my Air Conditioner modification.
The idea is to reach frequency doubling with triggering new square for upper and lower edges.
But this is the far target. :)

But what is the ESP01 pure performance ?


1. with LUA


ESP family CPU speed is 80MHz as default, but you can switch to 160Mhz which sound good but reality is disappointing output frequency is somewhere 6khz.
So lets start check what is max frequency generating LUA language:
node.setcpufreq(node.CPU160MHZ)
gpio.mode(4, gpio.OUTPUT)
tmr.wdclr()
for count = 1,102400 do
gpio.write(4,1)
gpio.write(4,0)
end
Conclusion: MAX frequency is ~6,6 KHz with LUA.
This is the theoretical max frequency with LUA.
Each new code line will slow down the out frequency.

For example if you make a longer sound loop the loop detection in NodeMCU will stop your loop after 5 sec. (as a workaround you have to reset the inner watchdog. but in this case freq. slow down.

node.setcpufreq(node.CPU160MHZ) gpio.mode(4, gpio.OUTPUT)
tmr.wdclr()
for count = 1,102400 do
gpio.write(4,1)
gpio.write(4,0)
end


So we have to use another language because LUA script language is slow for this.
Lets check micropython or Arduino IDE(C++)

2. with Micropython


Speed improvement almost double, but similar league in performance as LUA.

import pyb
import time
pin = pyb.Pin(2, pyb.Pin.OUT)
for i in range(4):
print('LED ON2')
pin.value(0)
time.sleep(1)
print('LED OFF2')
pin.value(1)
time.sleep(1)
print('2iteration done.')
print('All 2 done.')

3. Arduino IDE(C++)


Best performance -as expected. :)

Which is 20 times faster than LUA or 10 times faster than MicroPython.
#define ESP8266_LED 2
void setup()
{
pinMode(ESP8266_LED, OUTPUT);
}
void loop()
{
digitalWrite(ESP8266_LED, HIGH);
delayMicroseconds(3);
digitalWrite(ESP8266_LED, LOW);
delayMicroseconds(1);

}


In this article, we’ll cover the basics of the Lua programming language, including:

Let’s get started.

What is Lua?

Lua is a robust, lightweight, and embeddable scripting language that supports multiple programming methods, including procedural, object-oriented, functional, and data-driven programming.

As the primary focus on Lua is for scripting, it is rarely used as a standalone programming language. Instead, it is used as a scripting language that can be integrated (embedded) into other programs written in mainly C and C++. It also supports other programming languages via third-party plugins (NLua/KeraLua for .NET/C#).

Popular use cases for Lua include:

  • As a popular component in video game and game engine development. For example, Warframe, World of Warcraft, and CRYENGINE all use Lua.
  • As a programming language in many network programs, like CISCO Systems, Nmap, and ModSecurity.
  • As a programming language in industrial programs such as Adobe Lightroom and MySQL Workbench.
  • As a library that developers can integrate with their programs to enable scripting functionality.

Being a scripting language, Lua does not have its own main application. Instead, it acts exclusively as an embedded part of the host application.

(Explore the most popular programming languages.)

How Lua works

There are two main components of Lua:

  • The Lua interpreter
  • The Lua virtual machine (VM)

Lua is not directly interpreted through a Lua file like other languages such as Python. Instead, it uses the Lua interpreter to compile a Lua file to bytecode. The Lua interpreter is written in ANSI C, making it highly portable and capable of running on a multitude of devices.

Usually, the compilation is done at the runtime. However, sometimes it can be done before the runtime to increase load times. Then the Lua virtual machine will run this compiled bytecode. The register-based architecture of the Lua virtual machine closely resembles actual hardware architectures, and it will increase the overall performance of the program.

Key features of Lua

So, what’s so great about Lua? These are the defining features:

Speed

Lua is considered one of the fastest programming languages among interpreted scripting languages. In particular, Lua can perform large task orders faster than most other programming languages in both benchmarking and real-world scenarios.

For more speed, an independent implementation of Lua called LuaJIT uses a just-in-time compiler that makes Lua even faster.

Size

Lua has a considerably smaller footprint than other programming languages, with its complete source code and documentation taking a mere 1.3 MB. The Lua interpreter with all the standard libraries takes 278K, while the complete Lua library takes only 466K.

This small size is ideal when integrating Lua into multiple platforms, from embedded devices to massive game engines, where every byte is valuable.

Portability & embeddability

With its small size, the portability of Lua is nearly unlimited; any platform that supports the standard C compiler can run Lua out of the box. Lua’s speed and size become huge advantages when embedding Lua with another programming language. That’s because they can help increase the speed of the program without hindering any existing functionality.

Importantly, Lua does not require complex rewrites to be compatible with other programming languages. Lua can be used with primary programming languages like C, C++, Java, C#, etc.. and other scripting languages like Perl and Ruby, further extending its usability.

Simplicity

Lua is simple in design yet provides powerful functionality. One of the core features of Lua is meta-mechanisms which enable developers to implement features—rather than providing a bunch of features directly in the language itself.

Lua also comes with incremental garbage collection, reducing memory usage and implementation complexity. Its sandboxing feature can be used to isolate functions and resources. This increases the security of the program and provides coroutines for multitasking.

All these features come with a simple syntax and easily understandable format so that anyone can easily pick up Lua and use it in their programs.

License

Lua is free and open-source software distributed under the MIT license. This means anyone can use Lua for any purpose without paying any licensing or royalty fees.

Advantages & drawbacks

Like any language, Lua has its pros and cons.

Advantages of Lua

  • Easy app integration. Its high performance and small size make it easy to integrate Lua into applications.
  • Simple syntax. Relatively simple syntax structure with around 20 dedicated keywords, which helps to dive into Lua programming easily.
  • Flexibility. Without standard libraries, you can customize Lua to meet any need.
  • Cross-platform compatibility and support for the standard C compiler allows Lua to run virtually anywhere.
  • Dynamic variables in Lua allow defining variables without defining types, and the type is determined automatically at the runtime.
  • Easy debugging. Simple and powerful debug library.
  • Plenty of documentation. Comprehensive documentation to get Lua projects up and running quickly and the active community.

Disadvantages of Lua

  • Limited error handling support can lead to longer debug times to identify the exact errors in a Lua script.
  • All variables are created as global variables (global scope), which can lead to errors in variable assignments.
  • Limited pattern matching support.

When to use Lua

As a scripting language without major limitations, you can use Lua for any scenario, from a simple backend script in a web server to complex game development.

Lua is highly prevalent in video game development as it can be used to create functionality without contaminating the overall performance while also keeping everything separate.

Another area that Lua excels is embedded programming, where size and performance are major concerns. Lua can be used in everyday applications to extend the existing functionality or create new features and functions.

Lua Vs C++ Speed

Some popular games, programs, and services that use Lua are Dark Souls, Fable II, Garry’s Mod, Wireshark, VLC, Apache, and Nginx Web Servers.

Lua vs other languages

How does Lua stack up against other languages?

Lua Vs C++ Speed Test

Here’s a look at the differences between the high-level general programming language Python, the high-level object-oriented Java, and web-focused Javascript—all compared to Lua.

Installing Lua

Now, let’s see how to set up a development environment in Windows. First, we’ll install Lua.

Step 1

C++

Navigate to the Lua.org download page. Here, we will be using a precompiled binary to install Lua in windows. So, click on “get a binary link” as shown in the below screenshot.

Step 2

Click “Download” on the LuaBinaries page, and you will be redirected to a page with a list of precompiled binaries. Select the appropriate version from that list.

We will be using the latest Lua version for Windows 64 bit.

This will direct the user to a SourceForge page, where the binary will be downloaded.

Step 3

Move the downloaded Zip file to any location to store the binaries permanently.

Here, we will be using the “D:Program FilesLua” as the location. After moving the Zip file, simply extract its content using any compression utility (Ex: Windows Explorer, 7zip, WinRar).

Step 4

We need to add the location of Lua binaries to the system PATH so that Windows can call Lua from anywhere in the system.

Step 4.1. Navigate to Environment Variables. (Open Windows Explorer, right-click on This PC, and select properties.)

Step 4.2. Click on “Advanced System Settings” in the screen that appears and then click on “Environment Variables”.

Step 4.3. In the system variables section, add the location of the Lua executables as a new entry for the Path variable.


Step 5

Check if the system identifies Lua by opening up a command prompt or a PowerShell window and typing the Lua command (Lua with the version – lua54).

Setting up a Lua development environment

Now that we have installed Lua in the system, we need a development environment to go ahead with coding. For that, we can choose between:

  • A dedicated Lua IDE like ZeroBrane Studio
  • A general IDE like VSCode

We will be using VSCode for this instance.

Step 1

Let’s create a file called “lua_basic.lua” in VSCode and save that file in the desired location. Then we will type some print statements there like the following.


Step 2

Our Lua program needs to be compiled before running, so we need to create a Build Task. To do that, click on Terminal Menu, then Run Built Task and select the Configure Build Task Option.

( There will be different build task templates depending on the VSCode configuration. Select “Create tasks.json from template” and finally the “Others” option to define a custom build task.)

Run Build Task

Configure Build Task

Create tasks.json from template

Other

Step 2.1. We will add the following code block to configure the task. In that code block, we have defined a task called “Run Lua” that will run on the shell with the command “lua54”. That command will take the current file as the argument and carry out a build operation.

task.json

Lua Vs C++ Speed Download


Step 3

Lua Vs C++ Speed Calculator

Open up the “lua_basic.lua” file again. Then go to the “Terminal” again and click on “Run Build Task” or use the shortcut Ctrl+Shift+B.

This will compile the file and provide us with the output.

That’s it! Now we have a working Lua development environment that can be used to create Lua scripts. We can use the official Lua reference manual to explore the Lua language further.

Lua is powerful

Lua is a powerful scripting language that has limitless potential to add functionality to any program on a multitude of platforms to suit any use case.

Related reading