Prototype or Import, what's better (and faster)?

Just starting out? Need help? Post your questions and find answers here.
Max
User
User
Posts: 67
Joined: Thu Nov 30, 2006 4:57 pm
Location: I long for the absolute loneliness of the death

Prototype or Import, what's better (and faster)?

Post by Max »

Hi,
Somebody can explain me what construction is better and faster, for use dlls?:

Code: Select all

Prototype.l sql_init(mysql.l)
nlib = OpenLibrary(#PB_Any, "libmysql.lib")
mysql_init.sql_init=GetFunction(nlib, "mysql_init")
OR:

Code: Select all

Import "libmysql.lib"
mysql_init.l(*mysql.l)
I know the "theoretical"difference between them, but I dont know the "practical" difference between both approachs.

thanks @all
DarkDragon
Addict
Addict
Posts: 2344
Joined: Mon Jun 02, 2003 9:16 am
Location: Germany
Contact:

Post by DarkDragon »

Prototypes are dynamic, imports are static. For example in OpenGL I use the following routine to get the Shader functions:

Code: Select all

Prototype.l glDeleteObjectARB_(obj.l)
Prototype.l glGetHandleARB_(pname.l)
; ...

Global glDeleteObject_.glDeleteObjectARB_
Global glGetHandle_.glGetHandleARB_
; ...

Procedure InitGLSLShader()
  glDeleteObject_ = wglGetProcAddress_("glDeleteObjectARB")
  glGetHandle_ = wglGetProcAddress_("glGetHandleARB")
  ; ...
EndProcedure
But you need static (Imports) because you are able to bind them in the static way.
bye,
Daniel
Max
User
User
Posts: 67
Joined: Thu Nov 30, 2006 4:57 pm
Location: I long for the absolute loneliness of the death

Post by Max »

thanks, DarkDragon
Now, I know the difference, and I know that I need static imports.
But, there is some advantage in doing it on a way or another one?
I've read somewhere on this forum that Prototypes are faster, is 't true?
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Post by ts-soft »

Prototype and Import the same. Only the binding to a DLL is another. Import
makes a static Binding. It's faster than dynamic Binding, but if the DLL is
Missing, Windows gives the Error. Your Programm can't solve the Problem.
PureBasic 5.73 | SpiderBasic 2.30 | Windows 10 Pro (x64) | Linux Mint 20.1 (x64)
Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.
Image
Max
User
User
Posts: 67
Joined: Thu Nov 30, 2006 4:57 pm
Location: I long for the absolute loneliness of the death

Post by Max »

Thanks for the explanation.
Now it's clear for me. I will continue using IMPORT.
freak
PureBasic Team
PureBasic Team
Posts: 5940
Joined: Fri Apr 25, 2003 5:21 pm
Location: Germany

Post by freak »

Linking an import library is not faster than calling the dll functions directly, because
the import library also does an indirect call to the dll function. Basically it is
just doing the pointer work for you, but the dll is still loaded and called dynamically.
quidquid Latine dictum sit altum videtur
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post by Trond »

Loading would be faster with Import, not calling.
Post Reply