Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!

Using C/C++/FreeBASIC etc. dll with scripting languages.

EkBass

Bronze Coder
Hi.

I thought to try out to write simple FreeBASIC dll. I mostly use the neat GFX library of it with my dll. Via python/nodejs i can use it's power pretty easily.

I am just curious, has anyone tested is it possible to gain more calculation speed with such a dll? If we compare scripting languages and compiled languages in such a terms, it is a bit boring race due C and such whipes python/nodejs with a floor.

But, it takes its own computing power to transfer the calculations and all data from script to dll.

Before i spend couple of hours with this, i thought to ask has anyone tried here it yet?

If you want to try out, here is short example codes.

Code:
' freebasic
#cmdline "-dll"

declare function fb_screen     cdecl     alias     "fb_screen" (s as integer) as boolean
declare function fb_console cdecl     alias     "fb_console" () as boolean
declare function fb_print     cdecl     alias     "fb_print" (s as wstring) as boolean
declare function fb_sleep     cdecl     alias     "fb_sleep" (s as integer) as boolean
declare function fb_cls     cdecl     alias     "fb_cls" () as boolean

function fb_screen cdecl alias "fb_screen"(s as integer) as boolean export
    dim r_val as boolean = false
    if(s = 1 or s = 2 or s > 6 and s < 22) then
        screen s
        r_val = true
    end if
    return r_val
end function

function fb_console cdecl alias "fb_console" () as boolean export
    screen 0
    return true
end function

function fb_print cdecl alias "fb_print" (s as wstring) as boolean export
    print s;
    return true
end function

function fb_sleep cdecl alias "fb_sleep" (s as integer) as boolean export
    sleep s
    return true
end function

function fb_cls cdecl alias "fb_cls" () as boolean export
    cls
    return true
end function

JavaScript:
const ffi = require('ffi-napi');
const iconv = require('iconv-lite');
const path = require('path');

// Adjust the path to your shared library accordingly
const dllPath = path.join(__dirname, 'libfb-lib.so');

// Load the shared library
const libf = ffi.Library(dllPath, {
    'fb_screen': ['bool', ['int']],
    'fb_console': ['bool', []],
    'fb_print': ['bool', ['string']],
    'fb_sleep': ['bool', ['int']],
    'fb_cls': ['bool',[]],
});

// Call the 'fb_console' function
const result = libf.fb_console();

if (result) {
    libf.fb_cls();
    // Call the 'fb_print' function with a wide string
    const myString = "Hello from FreeBASIC";
    const myWideString = iconv.encode(myString, 'utf32-le');
    const resultPrint = libf.fb_print(myWideString);
} else {
    console.error("Printing failed");
}

    // Sleep for 5000 milliseconds
    const sleepResult = libf.fb_sleep(5000);
 
My past experience with dynamic loading and calling of DLL's/shared libraries is restricted to compiled languages and Java. No idea how interpreted languages like Python and Node.js handle this, but I can't believe there is much performance impact here. Perhaps the author of the ffi-napi package could tell you more ?
 
Yes, i did some simple tests here.

While, as expected pure C/Freebasic code beats scripting languages with ease, when using shared libraries the table is turned.

10000 3D coordinate calculations via shared library were around 5 times slower than stright with scripting language itself.
So, even if pure calculating power favors compiled languages, the type conversions between requests propably cause so significant slowdown, that in the end it goes much slower with shared libraries.
 
Last edited:
Yes, i did some simple tests here.

While, as expected pure C/Freebasic code beats scripting languages with ease, when using shared libraries the table is turned.

10000 3D coordinate calculations via shared library were around 5 times slower than stright with scripting language itself.
So, even if pure calculating power favors compiled languages, the type conversions between requests propably cause so significant slowdown, that in the end it goes much slower with shared libraries.
This is interesting. Makes one wonder how an interpreted language goes about calling native code does it not ? Again, I think the author of the ffi-napi package could possibly tell you more.

So are there specific things you can do with C/FreeBasic that you cannot do directly in Node.js or Python ? If not, I guess there is no problem. If yes, there is a problem that you likely cannot do anything about.
 
Nope, nothing so special i could not survive with scripting languages or compiled ones alone. But i am curious mind. But using FreeBASIC gfx library is something i most likely use in future.
 
Back
Top Bottom