Last active
December 16, 2023 17:33
-
-
Save azat/aad8c509355970e3f09fe90279f7825d to your computer and use it in GitHub Desktop.
MADV_FREE vs MADV_DONTNEED benchmark
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import mmap | |
import time | |
NUMBER_VMAS = 10000 | |
VMA_SIZE = 1<<20 | |
buffer = b'0' * VMA_SIZE | |
def make_vma(): | |
vma = mmap.mmap(-1, VMA_SIZE, mmap.MAP_PRIVATE, mmap.MADV_NOHUGEPAGE) | |
vma.write(buffer) | |
return vma | |
def elapsed_ns(func): | |
start = time.time_ns() | |
func() | |
end = time.time_ns() | |
return end - start | |
def main(): | |
vmas = [make_vma() for _ in range(NUMBER_VMAS)] | |
def run_MADV_FREE(): | |
for vma in vmas: | |
vma.madvise(mmap.MADV_FREE) | |
e = elapsed_ns(run_MADV_FREE) | |
print('MADV_FREE: {:.3f} us'.format(e / 1e3)) | |
del vmas | |
vmas = [make_vma() for _ in range(NUMBER_VMAS)] | |
def run_MADV_DONTNEED(): | |
for vma in vmas: | |
vma.madvise(mmap.MADV_DONTNEED) | |
e = elapsed_ns(run_MADV_DONTNEED) | |
print('MADV_DONTNEED: {:.3f} us'.format(e / 1e3)) | |
del vmas | |
if __name__ == "__main__": | |
main() |
Author
azat
commented
Dec 16, 2023
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment