C写档速度问题
我有一支VB6程序连续调用VC6编译的DLL档,再由VB6的介面计算每次写二进制档的执行速度,发现不知道为什么,每次写档前约30次,花费时间几乎为之后写档速度的 2倍,
无论档案是新开的或是Append的。想不出是什么原因而决定这个结果?
VB6 :
程序代码:
Option Explicit Private Declare Function Write1 Lib "Write1.dll" (ByVal FN As String, ByVal iCount As Long, ByVal A As Long) As Long Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long) Private Declare Function GetSystemDirectory Lib "kernel32" Alias "GetSystemDirectoryA" (ByVal lpbuffer As String, ByVal nSize As Long) As Long Private Declare Function SetCurrentDirectory Lib "kernel32" Alias "SetCurrentDirectoryA" (ByVal lpPathName As String) As Long Private Declare Function QueryPerformanceCounter Lib "kernel32" (X As Currency) As Boolean Private Declare Function QueryPerformanceFrequency Lib "kernel32" (X As Currency) As Boolean Dim S1 As Currency, S2 As Currency, f As Currency, t1 As Currency Dim iSize(25600) As Long, Myindex As Integer, iTime(25600) As Currency, iCount As Integer Private Sub Form_Activate() Dim i As Integer List1.Clear List2.Clear For i = 0 To 25600 List1.AddItem i & " : " & iSize(i), i List2.AddItem i & " : " & iTime(i), i Next i End Sub Private Sub Form_Load() Dim i As Integer, A() As Byte, j As Integer, k As Long For i = 0 To 25600 ReDim A(256) For j = 0 To 256 A(j) = i Mod 256 Next j Call QueryPerformanceFrequency(f) Call QueryPerformanceCounter(S1) Call WaitEXE(Write1("C:\AAA.std", 256, VarPtr(A(0))), iCount) Call QueryPerformanceCounter(S2) iTime(iCount) = (S2 - S1) / f iCount = iCount + 1 Next i End Sub Private Sub WaitEXE(k As Long, m As Integer) Do If k = 1 Then iSize(m) = k Exit Do End If MyDoEvents 100 Loop End Sub Private Function MyDoEvents(Optional ByVal dwMilliseconds As Long = 1) MyDoEvents = DoEvents() Sleep dwMilliseconds End Function
VC6 :
程序代码:
// Write1.cpp : Defines the entry point for the DLL application. // #include "stdafx.h" #include <stdio.h> BOOL APIENTRY DllMain( HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved ) { return TRUE; } int _stdcall Write1(char *FN, int Count, int *A) { int i=0,nwDataSize=0; long size=0; FILE *stream = fopen( FN, "ab+" ); if( stream != NULL ) { fseek(stream,0,SEEK_END); size=ftell(stream); nwDataSize =fwrite(&A[0],sizeof (char), Count, stream); } else return 0; fclose(stream); return 1; }
0.0059秒 <.....> 0.0021秒
[此贴子已经被作者于2020-4-29 14:03编辑过]