#!/usr/bin/env python import io import _pyio import time COPY_SIZE = 1024**3 BUF_SIZE = 16384 ofh = open('/dev/null', 'wb') ifh_r = open('/dev/zero', 'rb', buffering=0) for (label, ifh) in (('C', io.BufferedReader(ifh_r)), ('Python', _pyio.BufferedReader(ifh_r))): buf = bytearray(BUF_SIZE) stamp = time.time() copied = 0 while copied < COPY_SIZE: len_ = ifh.readinto1(buf) ofh.write(buf[:len_]) copied += len_ print('%s readinto1: %.3e seconds' % (label, time.time() - stamp,)) stamp = time.time() copied = 0 while copied < COPY_SIZE: buf = ifh.read1(BUF_SIZE) ofh.write(buf) copied += len(buf) print('%s read1: %.3e seconds' % (label, time.time() - stamp,)) buf = bytearray(BUF_SIZE) stamp = time.time() copied = 0 while copied < COPY_SIZE: len_ = ifh.readinto(buf) ofh.write(buf[:len_]) copied += len_ print('%s readinto: %.3e seconds' % (label, time.time() - stamp,)) stamp = time.time() copied = 0 while copied < COPY_SIZE: buf = ifh.read(BUF_SIZE) ofh.write(buf) copied += len(buf) print('%s read: %.3e seconds' % (label, time.time() - stamp,))