Hi all,
I'm curious as to the technicalities of why a particular method declaring and calling is
faster than the other. The C syntax is:
\\\\\
BOOL PathMatchSpec(
LPCSTR pszFile,
LPCSTR pszSpec
);
\\\\\
In VB2005, I'm declaring and then calling an API function as such:
\\\\\
Private Declare Auto Function PathMatchSpec Lib "shlwapi" _
(ByVal pszFileParam As IntPtr, _
ByVal pszSpec As IntPtr) As Boolean
Private Function MatchSpec(ByVal sFile As String, ByVal sSpec As String) As Boolean
Dim FilePtr As IntPtr = Marshal.StringT oHGlobalAuto(sF ile)
Dim SpecPtr As IntPtr = Marshal.StringT oHGlobalAuto(sS pec)
Dim Match As Boolean
Match = PathMatchSpec(F ilePtr, SpecPtr)
Marshal.FreeHGl obal(FilePtr)
Marshal.FreeHGl obal(SpecPtr)
Return Match
End Function
\\\\\
This (the above example) runs nearly 20% faster over the course of about 190,000 calls
than declaring and calling it as such:
\\\\\
Private Declare Auto Function PathMatchSpec Lib "shlwapi" _
(ByVal pszFileParam As String, _
ByVal pszSpec As String) As Boolean
Private Function MatchSpec(ByVal sFile As String, ByVal sSpec As String) As Boolean
Return PathMatchSpec(s File, sSpec)
End Function
\\\\\
With all the marshaling in the first example, I would have thought there would have been
some significant overhead going on compared to the second example. 4 1556
Lance wrote:
Hi all,
I'm curious as to the technicalities of why a particular method declaring and calling is
faster than the other. The C syntax is:
\\\\\
BOOL PathMatchSpec(
LPCSTR pszFile,
LPCSTR pszSpec
);
\\\\\
In VB2005, I'm declaring and then calling an API function as such:
\\\\\
Private Declare Auto Function PathMatchSpec Lib "shlwapi" _
(ByVal pszFileParam As IntPtr, _
ByVal pszSpec As IntPtr) As Boolean
Private Function MatchSpec(ByVal sFile As String, ByVal sSpec As String) As Boolean
Dim FilePtr As IntPtr = Marshal.StringT oHGlobalAuto(sF ile)
Dim SpecPtr As IntPtr = Marshal.StringT oHGlobalAuto(sS pec)
Dim Match As Boolean
Match = PathMatchSpec(F ilePtr, SpecPtr)
Marshal.FreeHGl obal(FilePtr)
Marshal.FreeHGl obal(SpecPtr)
Return Match
End Function
\\\\\
This (the above example) runs nearly 20% faster over the course of about 190,000 calls
Can you please post a complete example of this (including your method
of timing)? I am not seeing much of a difference at all. Over a
190,000 calls I'm only seeing about a .00008 second difference...
--
Tom Shelton
Hi Tom,
I knew you'd be the first to reply to an API question :-)
My method of timing uses:
\\\\\
Private Declare Auto Function GetTickCount Lib "kernel32" () As Int32
\\\\\
right before and after the start of the loop, then calculates the diference.
BTW, I'm an idiot and I missposted something: 190,000 returned True from the function
call, but the function iteself was called 1,040,528 times. Also, LAN conditions are
likely effecting this as well; the 20% figure I gave earlier is an average over the course
of testing on different days at different times.
FWIW, the loop is not a test loop (i.e., x = 1 to 190000) but a loop that includes
searching for all files (*.*) on a network of which 190,000 fit the sSpec and return True.
Also, the sSpec string contains 44 different file types (i.e. "*. txt; *.log; *.
doc;....." etc.). I suppose these factors may effect the results.
As for the rest of the code, it's a VB.Net-ized version of http://vbnet.mvps.org/index.html?cod...l_multiple.htm. Not
all that much has changed from the code you see on that page, except for the types and the
declarations (which you provided in a previous answer to a posting of mine), including the
variations I posted in my original message.
Lance
"Tom Shelton" <to*@mtogden.co mwrote in message
news:11******** *************@e 3g2000cwe.googl egroups.com...
>
Lance wrote:
>Hi all,
I'm curious as to the technicalities of why a particular method declaring and calling is faster than the other. The C syntax is:
\\\\\ BOOL PathMatchSpec( LPCSTR pszFile, LPCSTR pszSpec ); \\\\\
In VB2005, I'm declaring and then calling an API function as such:
\\\\\ Private Declare Auto Function PathMatchSpec Lib "shlwapi" _ (ByVal pszFileParam As IntPtr, _ ByVal pszSpec As IntPtr) As Boolean
Private Function MatchSpec(ByVal sFile As String, ByVal sSpec As String) As Boolean Dim FilePtr As IntPtr = Marshal.StringT oHGlobalAuto(sF ile) Dim SpecPtr As IntPtr = Marshal.StringT oHGlobalAuto(sS pec) Dim Match As Boolean Match = PathMatchSpec(F ilePtr, SpecPtr) Marshal.FreeHG lobal(FilePtr) Marshal.FreeHG lobal(SpecPtr) Return Match End Function \\\\\
This (the above example) runs nearly 20% faster over the course of about 190,000 calls
Can you please post a complete example of this (including your method
of timing)? I am not seeing much of a difference at all. Over a
190,000 calls I'm only seeing about a .00008 second difference...
--
Tom Shelton
Lance,
I don't think it's a very good idea to involve network IO in your
benchmark. There are so many other factors that affect performance
when you involve a network connection (such as caching, network load
etc).
I tried code similar to yours on different declarations of another API
- lstrcmp - that has almost the same signature as PathMatchSpec.
Declare Auto Function lstrcmp1 Lib "kernel32.d ll" Alias "lstrcmp"
(ByVal lpString1 As String, ByVal lpString2 As String) As Integer
Declare Auto Function lstrcmp2 Lib "kernel32.d ll" Alias "lstrcmp"
(<MarshalAs(Unm anagedType.LPTS tr)ByVal lpString1 As String,
<MarshalAs(Unma nagedType.LPTSt r)ByVal lpString2 As String) As
Integer
Declare Auto Function lstrcmp3 Lib "kernel32.d ll" Alias "lstrcmp"
(ByVal lpString1 As IntPtr, ByVal lpString2 As IntPtr) As Integer
For 1,000,000 iterations timed with the .NET 2.0 StopWatch class I got
the following results
1: 882 msec
2: 198 msec
3: 1525 msec
So in this case the clear winner is declaring the parameters as String
and adding the MarshalAs(LPTSt r) attribute to avoid VB's default
MarshalAs(VBByr efStr) which causes unnecessary copying to preserve
classic VB behaviour. The third, DIY method is by far the slowest like
I would expect, since it involves the most managed/native transitions.
Mattias
--
Mattias Sjögren [C# MVP] mattias @ mvps.org http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Thanks for the info Mattias.
Lance
"Mattias Sjögren" <ma************ ********@mvps.o rgwrote in message
news:%2******** ********@TK2MSF TNGP04.phx.gbl. ..
Lance,
I don't think it's a very good idea to involve network IO in your
benchmark. There are so many other factors that affect performance
when you involve a network connection (such as caching, network load
etc).
I tried code similar to yours on different declarations of another API
- lstrcmp - that has almost the same signature as PathMatchSpec.
Declare Auto Function lstrcmp1 Lib "kernel32.d ll" Alias "lstrcmp"
(ByVal lpString1 As String, ByVal lpString2 As String) As Integer
Declare Auto Function lstrcmp2 Lib "kernel32.d ll" Alias "lstrcmp"
(<MarshalAs(Unm anagedType.LPTS tr)ByVal lpString1 As String,
<MarshalAs(Unma nagedType.LPTSt r)ByVal lpString2 As String) As
Integer
Declare Auto Function lstrcmp3 Lib "kernel32.d ll" Alias "lstrcmp"
(ByVal lpString1 As IntPtr, ByVal lpString2 As IntPtr) As Integer
For 1,000,000 iterations timed with the .NET 2.0 StopWatch class I got
the following results
1: 882 msec
2: 198 msec
3: 1525 msec
So in this case the clear winner is declaring the parameters as String
and adding the MarshalAs(LPTSt r) attribute to avoid VB's default
MarshalAs(VBByr efStr) which causes unnecessary copying to preserve
classic VB behaviour. The third, DIY method is by far the slowest like
I would expect, since it involves the most managed/native transitions.
Mattias
--
Mattias Sjögren [C# MVP] mattias @ mvps.org http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Michael Tobis |
last post by:
Summary of my understanding of a recent interesting thread:
General usage has "declaration" meaning "statement which does not
generate executable bytecode but merely affects the compiler". My
assertion that decorator syntax is "declarative" is therefore formally
false.
The common assertion that "Python is 100% executable" is an
exageration, but not because of the introduction of decorator syntax.
The "global" statement, however is not...
|
by: aleko |
last post by:
This applies equally to function prototypes. Why do we have to tell the
compiler twice? Other modern compiled languages don't have this
requirement.
Just curious,
Aleko
|
by: James A. Donald |
last post by:
I am contemplating getting into Python, which is used by engineers I
admire - google and Bram Cohen, but was horrified to read
"no variable or argument declarations are necessary."
Surely that means that if I misspell a variable name, my program will
mysteriously fail to work with no error message.
If you don't declare variables, you can inadvertently re-use an
variable used in an enclosing context when you don't intend to, or
|
by: Arthur J. O'Dwyer |
last post by:
Well, I'm trying to write that program that was requested
a few weeks back, the one that could take struct definitions
and create portable functions to read and write those
structs. Hence the 'savestruct' in the subject line.
I cannot for the life of me figure out how to parse C
declarations correctly! I've come up with an intermediate
format that I'd really like to keep, if at all possible,
which involves making a linked list (or...
|
by: Michael B. |
last post by:
I tend to use rather descriptive names for parameters, so the old style of
declaration appeals to me, as I can keep a declaration within 80 chars:
void * newKlElem (frame_size,num_blocks,num_frames,frame_locator)
size_t frame_size;
unsigned short num_blocks;
unsigned short num_frames;
Kl_frame_locator *locator;
{
/* code goes here */
| |
by: Simon Brooke |
last post by:
I was debugging a new XML generator tonight and trying to determine why
it wasn't working; and realised my dom printer does not output XML
namespace declarations.
My method to output an Element is as follows:
/**
* Print an element node, and, by recursive descent, it's children
*
* @param node the node to print
|
by: raylopez99 |
last post by:
I'm having problems compiling complex reference declarations in
MSVC++.NET 2002 IDE.
Here is an example:
// --Foo.h--
#include "Bar.h"
class Bar; //forward decl. to a class Bar in another file, not used
|
by: Juha Nieminen |
last post by:
I once made my own smart pointer implementation for a project and
at one point fought to death to find a malicious bug. The program
was not working and I couldn't figure out why.
The source of the problem was that MSVC++ was not giving me a
warning even though it should have. The problem was that I was
creating a smart pointer from a forward-declaration of a class
which, it seems, makes it impossible for the smart pointer to call
the...
|
by: John Williams |
last post by:
I've been spending some time expanding my rather primitive knowledge of
c++ through various exercises. One of them that I've been working on is
learning a bit about how compression works. I've started with a huffman
compression algorithm and ran into this example code to get me started:
http://www.flipcode.com/cgi-bin/fcarticles.cgi?show=64109
Unfortunately I have some questions as to why certain things where done
or what they mean, as...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |