Skip to content

Instantly share code, notes, and snippets.

@shirosaki
Created December 5, 2011 09:39
Show Gist options
  • Save shirosaki/1433038 to your computer and use it in GitHub Desktop.
Save shirosaki/1433038 to your computer and use it in GitHub Desktop.
tma sample
diff --git a/samples/file_access.cpp b/samples/file_access.cpp
new file mode 100644
index 0000000..1ea02e3
--- /dev/null
+++ b/samples/file_access.cpp
@@ -0,0 +1,108 @@
+#include <ios>
+#include <iostream>
+#include <ostream>
+#include <iomanip>
+#include <stdexcept>
+
+#if defined (_WIN32)
+# include <windows.h>
+# include <stdio.h>
+# include <fcntl.h>
+# define executable "file_access.exe"
+# define access _access
+# define open _open
+# define O_RDONLY _O_RDONLY
+# include <sys/stat.h>
+# define stat _stat
+#else
+# include <sys/types.h>
+# include <sys/stat.h>
+# include <fcntl.h>
+# include <unistd.h>
+# define executable "file_access"
+#endif
+
+#include "hrtimer.h"
+
+int main(int argc, char* argv[])
+{
+ using namespace std;
+
+ double rv = 0.0;
+
+ // hires timer returning usecond times
+ HiRes::Timer<HiRes::us> t_us;
+
+ string headers[] = {"file exist", "file not exist"};
+ string file_path[] = {executable, "dummy"};
+ for (int i = 0; i < 2; i++) {
+ const char *file = file_path[i].c_str();
+ cout << endl << headers[i] << endl;
+ t_us.start();
+ access(file, 4);
+ rv = t_us.stop();
+ cout << left
+ << setw(30)
+ << "access() time: "
+ << rv << endl;
+
+ t_us.start();
+ int fd = open(file, O_RDONLY);
+ if (fd != -1) close(fd);
+ rv = t_us.stop();
+ cout << left
+ << setw(30)
+ << "open() time: "
+ << rv << endl;
+
+
+ struct stat statbuf;
+ t_us.start();
+ stat(file, &statbuf);
+ rv = t_us.stop();
+ cout << left
+ << setw(30)
+ << "stat() time: "
+ << rv << endl;
+
+#if defined(_WIN32)
+ OFSTRUCT openbuf;
+ t_us.start();
+ HFILE hFile = OpenFile(file, &openbuf, OF_READ);
+ if (hFile != HFILE_ERROR) CloseHandle((HANDLE) hFile);
+ rv = t_us.stop();
+ cout << left
+ << setw(30)
+ << "OpenFile() time: "
+ << rv << endl;
+
+ t_us.start();
+ HANDLE h = CreateFile(file, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE,
+ NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
+ if (h != INVALID_HANDLE_VALUE) CloseHandle(h);
+ rv = t_us.stop();
+ cout << left
+ << setw(30)
+ << "CreateFile() time: "
+ << rv << endl;
+
+ WIN32_FILE_ATTRIBUTE_DATA fileInfo;
+ t_us.start();
+ GetFileAttributesEx(file, GetFileExInfoStandard, &fileInfo);
+ rv = t_us.stop();
+ cout << left
+ << setw(30)
+ << "GetFileAttributesEx() time: "
+ << rv << endl;
+
+ t_us.start();
+ GetFileAttributes(file);
+ rv = t_us.stop();
+ cout << left
+ << setw(30)
+ << "GetFileAttributes() time: "
+ << rv << endl;
+#endif
+ }
+
+ return 0;
+}
diff --git a/samples/wscript b/samples/wscript
index 0d69362..c6f457b 100644
--- a/samples/wscript
+++ b/samples/wscript
@@ -25,4 +25,12 @@ def build(bld):
linkflags = my_linkflags,
)
+ bld.program(
+ source = [ 'file_access.cpp' ],
+ includes = [ '../include' ],
+ target = 'file_access',
+ cxxflags = my_cxxflags,
+ linkflags = my_linkflags,
+ )
+
# vim: ft=python ai ts=4 sw=4 sts=4 et
@shirosaki
Copy link
Author

# Windows XP
file exist
access() time:                28.6633
open() time:                  97.6639
stat() time:                  219.292
OpenFile() time:              324.711
CreateFile() time:            91.1813
GetFileAttributesEx() time:   24.6656
GetFileAttributes() time:     22.9201

file not exist
access() time:                22.237
open() time:                  91.0049
stat() time:                  205.485
OpenFile() time:              1486.96
CreateFile() time:            88.9103
GetFileAttributesEx() time:   21.7302
GetFileAttributes() time:     21.4524

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment