Before answering your question let me mention that writing to a file using API in Windows consists of following (simplified) stages:
- You call
WriteFile
(kernel32.dll)
WriteFile
calls NtWriteFile
(ntdll.dll)
NtWriteFile
calls SYSENTER
and operation proceeds to kernel mode
- In kernel mode
NtWriteFile
function of Ntoskrnl.exe is called
- This sends
IRP_MJ_WRITE
to file system driver
- File system driver determines which sectors should be written and passes to storage driver
- Storage driver sends a command to the hard drive to actually write data to specified sectors
- Hard drive writes the data
All operations 1 to 7 are very fast compared to 8 (unless you are working with a RAM drive or extremely fast SSD)
Method 1 - You can skip Step 1 easily (by calling NtWriteFile
), and Step2 (by calling SYSENTER
- not easy). However you will not gain any performance improvement, so no point in doing it. Consider WriteFile
just a wrapper for those (I don't think you are after eliminating one extra function call).
Method 2 - you can find out which sectors the file occupies and write to them directly (effectively skipping all steps down to Step 7). To do that you will need to open and lock the volume, find the clusters that the target file occupies by FSCTL_GET_RETRIEVAL_POINTERS
call, and call WriteFile
on volume handle.
But it will be unfair comparison, because file system driver not only writes to the data sectors, but also updates file system metadata when you call WriteFile.
Bottom line is - "Testing efficiency over win32 API" doesn't make much sense. You can skip some of the stuff that OS does, but either won't give you any difference in speed (method 1), or there will be unfair comparison (method2).