Skip to content

Instantly share code, notes, and snippets.

@suzaku
Last active December 21, 2015 04:09
Show Gist options
  • Save suzaku/6247136 to your computer and use it in GitHub Desktop.
Save suzaku/6247136 to your computer and use it in GitHub Desktop.
O_DSYNC vs. O_SYNC vs. Call fsync manually vs. no sync
In [19]: fd = os.open('t1', os.O_CREAT | os.O_DSYNC | os.O_WRONLY)
In [20]: %timeit os.write(fd, 'a')
1000 loops, best of 3: 232 us per loop
In [21]: fd2 = os.open('t2', os.O_CREAT | os.O_SYNC | os.O_WRONLY)
In [22]: %timeit os.write(fd2, 'a')
1000 loops, best of 3: 737 us per loop
In [36]: fd3 = os.open('t3', os.O_CREAT | os.O_WRONLY)
In [37]: %timeit os.write(fd3, 'a'); os.fsync(fd3)
1000 loops, best of 3: 740 us per loop
In [38]: fd4 = os.open('t4', os.O_CREAT | os.O_WRONLY)
In [39]: %timeit os.write(fd4, 'a')
100000 loops, best of 3: 2.32 us per loop
In [41]: ls -lh t[1,2,3,4]
-rwxrwxr-x 1 vagrant vagrant 4.1K Aug 16 11:32 t1*
-rwxrwxr-x 1 vagrant vagrant 4.1K Aug 16 11:32 t2*
-rwxrwxr-x 1 vagrant vagrant 4.1K Aug 16 11:38 t3*
-rwxrwxr-x 1 vagrant vagrant 402K Aug 16 11:40 t4*
In [92]: data = 'a'*(32*1024)
In [93]: fd5 = os.open('t5', os.O_CREAT | os.O_DSYNC | os.O_WRONLY)
In [94]: %timeit os.write(fd5, data)
1000 loops, best of 3: 825 us per loop
In [95]: fd6 = os.open('t6', os.O_CREAT | os.O_SYNC | os.O_WRONLY)
In [96]: %timeit os.write(fd6, data)
1000 loops, best of 3: 831 us per loop
@flaneur2020
Copy link

In [1]: data = 'a'*(32*1024)

In [4]: fd5 = os.open('t5', os.O_CREAT | os.O_DSYNC | os.O_WRONLY)

In [5]: %timeit os.write(fd5, data)
10 loops, best of 3: 34.6 ms per loop

In [6]: fd6 = os.open('t6', os.O_CREAT | os.O_SYNC | os.O_WRONLY)

In [8]: %timeit os.write(fd6, data)
10 loops, best of 3: 33.7 ms per loop

@flaneur2020
Copy link

In [9]: data = 'a'

In [10]: %timeit os.write(fd5, data)
10 loops, best of 3: 33.5 ms per loop

In [11]: %timeit os.write(fd6, data)
10 loops, best of 3: 33.5 ms per loop

@suzaku
Copy link
Author

suzaku commented Aug 16, 2013

我的电脑好好啊!

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