Speed of Code? 
Author Message
 Speed of Code?

Hi,

I want to compare speed for some operation between VC and
VB code.

I found the speed of the calculation of VC code is much
faster than VB code. But there are two issues I could not
understand.

First, the speed of reading text file (please see the
following codes):
// VC-Code
m_pStream = fopen("data_VC.txt", "r");
m_ch=fgetc(m_pStream);
m_ch=fgetc(m_pStream);
fclose(m_pStream);

' VB-Code
Open m_strFileName For Input As #1
Input #1, Chr1
Input #1, Chr1
Close #1

Those codes run 20000 times. The run time for VC code is
14 seconds, and the run time for VB code is 15 seconds.
The difference between them is too small. Why? Is there
another faster method to read from text file in VC?

Second, the speed of loading database (please see the
following codes):

// VC code
// m_datSpeed is a class (CspeedData) generated by
// VC ATL object wizard, and CspeedData declared as
// class CSpeedData : public CCommand<CAccessor<...>>
// My project is with MFC and Automation surport.
m_datSpeed.Open();
m_datSpeed.MoveNext();
sprintf(strData,
    "%s|%d|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s",
    m_datSpeed.m_CPU,
    m_datSpeed.m_ID,
    m_datSpeed.m_RAM,
    m_datSpeed.m_ScreenResolution,
    m_datSpeed.m_TimeOfRun,
    m_datSpeed.m_TimeOfTesting,
    m_datSpeed.m_TimerStatus,
    m_datSpeed.m_TimesOfChangeProp,
    m_datSpeed.m_TimesOfFloatOperation,
    m_datSpeed.m_TimesOfIntOperation,
    m_datSpeed.m_TimesOfLoadDatabase,
    m_datSpeed.m_TimesOfMoveImage,
    m_datSpeed.m_TimesOfOpenFile,
    m_datSpeed.m_TimesOfReadChar,
    m_datSpeed.m_TimesOfRunIf,
    m_datSpeed.m_TimesOfSendMessage );
m_datSpeed.m_session.Close();
m_datSpeed.Close();

VB code
Set m_cnnSpeed = New ADODB.Connection
m_cnnSpeed.Open m_strCNN
Set m_rstSpeed = m_cnnSpeed.Execute("select * from
SpeedData")
With m_rstSpeed
    m_strTemp = m_rstSpeed(0)
    m_strTemp = m_strTemp & "|" & m_rstSpeed(1)
    m_strTemp = m_strTemp & "|" & m_rstSpeed(2)
    m_strTemp = m_strTemp & "|" & m_rstSpeed(3)
    m_strTemp = m_strTemp & "|" & m_rstSpeed(4)
    m_strTemp = m_strTemp & "|" & m_rstSpeed(5)
    m_strTemp = m_strTemp & "|" & m_rstSpeed(6)
    m_strTemp = m_strTemp & "|" & m_rstSpeed(7)
    m_strTemp = m_strTemp & "|" & m_rstSpeed(8)
    m_strTemp = m_strTemp & "|" & m_rstSpeed(9)
    m_strTemp = m_strTemp & "|" & m_rstSpeed(10)
    m_strTemp = m_strTemp & "|" & m_rstSpeed(11)
    m_strTemp = m_strTemp & "|" & m_rstSpeed(12)
    m_strTemp = m_strTemp & "|" & m_rstSpeed(13)
    m_strTemp = m_strTemp & "|" & m_rstSpeed(14)
    m_strTemp = m_strTemp & "|" & m_rstSpeed(15)
End With
Call CloseRecordset(m_rstSpeed)
Call CloseConnection(m_cnnSpeed)

The database is Access 2000 database. Those codes run 500
times. The run time for VC code is 20 seconds, and the run
time for VB code is 25 seconds. The difference between
them is also too small. Why? Did I used not right way to
load database in VC code ?

If some people know how to improve the speed of those VC
codes, please let me know.

Thanks in advance!

David



Sat, 11 Sep 2004 02:22:27 GMT  
 Speed of Code?
You are operating under the wrong impression VC is much faster than VB.
This is a popular myth, but it isn't true. On average, VC programs are
faster than VB programs but this doesn't come from individual comparisons
like the ones you make, rather from the cumbersome design of the VB
runtime and the fact you have to work around it so many times.

In a nutshell, calculations (if properly written) in VB are not any slower
than in VC (your VB computation code is written sloppy, perhaps you don't
use strongly/correctly typed constants). File access makes no difference
as it is code in the OS. Database access - you measure the performance
of ADO vs OLE DB. Since ADO is built on top of OLE DB, it's normal for it
to be slightly slower. Try writing your C++ code with ADO wrappers
and you'll see the performance equivalent to that in VB (if not lower).

The superiority of C++ comes from the fact you may use broader and
better techniques when writing your code, luxury you rarely have in VB.
ADO vs OLE DB above is just such an example...

--
=====================================
Alexander Nickolov
Microsoft MVP [VC], MCSD

MVP VC FAQ: http://www.mvps.org/vcfaq
=====================================

Quote:

> Hi,

> I want to compare speed for some operation between VC and
> VB code.

> I found the speed of the calculation of VC code is much
> faster than VB code. But there are two issues I could not
> understand.

> First, the speed of reading text file (please see the
> following codes):
> // VC-Code
> m_pStream = fopen("data_VC.txt", "r");
> m_ch=fgetc(m_pStream);
> m_ch=fgetc(m_pStream);
> fclose(m_pStream);

> ' VB-Code
> Open m_strFileName For Input As #1
> Input #1, Chr1
> Input #1, Chr1
> Close #1

> Those codes run 20000 times. The run time for VC code is
> 14 seconds, and the run time for VB code is 15 seconds.
> The difference between them is too small. Why? Is there
> another faster method to read from text file in VC?

> Second, the speed of loading database (please see the
> following codes):

> // VC code
> // m_datSpeed is a class (CspeedData) generated by
> // VC ATL object wizard, and CspeedData declared as
> // class CSpeedData : public CCommand<CAccessor<...>>
> // My project is with MFC and Automation surport.
> m_datSpeed.Open();
> m_datSpeed.MoveNext();
> sprintf(strData,
>     "%s|%d|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s",
>     m_datSpeed.m_CPU,
>     m_datSpeed.m_ID,
>     m_datSpeed.m_RAM,
>     m_datSpeed.m_ScreenResolution,
>     m_datSpeed.m_TimeOfRun,
>     m_datSpeed.m_TimeOfTesting,
>     m_datSpeed.m_TimerStatus,
>     m_datSpeed.m_TimesOfChangeProp,
>     m_datSpeed.m_TimesOfFloatOperation,
>     m_datSpeed.m_TimesOfIntOperation,
>     m_datSpeed.m_TimesOfLoadDatabase,
>     m_datSpeed.m_TimesOfMoveImage,
>     m_datSpeed.m_TimesOfOpenFile,
>     m_datSpeed.m_TimesOfReadChar,
>     m_datSpeed.m_TimesOfRunIf,
>     m_datSpeed.m_TimesOfSendMessage );
> m_datSpeed.m_session.Close();
> m_datSpeed.Close();

> &#8216; VB code
> Set m_cnnSpeed = New ADODB.Connection
> m_cnnSpeed.Open m_strCNN
> Set m_rstSpeed = m_cnnSpeed.Execute("select * from
> SpeedData")
> With m_rstSpeed
>     m_strTemp = m_rstSpeed(0)
>     m_strTemp = m_strTemp & "|" & m_rstSpeed(1)
>     m_strTemp = m_strTemp & "|" & m_rstSpeed(2)
>     m_strTemp = m_strTemp & "|" & m_rstSpeed(3)
>     m_strTemp = m_strTemp & "|" & m_rstSpeed(4)
>     m_strTemp = m_strTemp & "|" & m_rstSpeed(5)
>     m_strTemp = m_strTemp & "|" & m_rstSpeed(6)
>     m_strTemp = m_strTemp & "|" & m_rstSpeed(7)
>     m_strTemp = m_strTemp & "|" & m_rstSpeed(8)
>     m_strTemp = m_strTemp & "|" & m_rstSpeed(9)
>     m_strTemp = m_strTemp & "|" & m_rstSpeed(10)
>     m_strTemp = m_strTemp & "|" & m_rstSpeed(11)
>     m_strTemp = m_strTemp & "|" & m_rstSpeed(12)
>     m_strTemp = m_strTemp & "|" & m_rstSpeed(13)
>     m_strTemp = m_strTemp & "|" & m_rstSpeed(14)
>     m_strTemp = m_strTemp & "|" & m_rstSpeed(15)
> End With
> Call CloseRecordset(m_rstSpeed)
> Call CloseConnection(m_cnnSpeed)

> The database is Access 2000 database. Those codes run 500
> times. The run time for VC code is 20 seconds, and the run
> time for VB code is 25 seconds. The difference between
> them is also too small. Why? Did I used not right way to
> load database in VC code ?

> If some people know how to improve the speed of those VC
> codes, please let me know.

> Thanks in advance!

> David



Sat, 11 Sep 2004 04:01:33 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Bad Address from Speed Optimized Code

2. How to optimize execution speed of code ??

3. Use of static to speed up code?

4. Help! A beginner out in speeding up code

5. Speed of Code?

6. Problems with for speed optimized code

7. How can I speed up code editing?

8. Problems with for speed optimized code

9. Speed of Code?

10. VBCE X VCCE - SPEED and CODE SIZE

11. For temp array allocation in speed critcal code : should I use _alloca ?

12. Speed difference in Managed and Unmanaged code

 

 
Powered by phpBB® Forum Software