-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuffer.py
49 lines (36 loc) · 1.09 KB
/
buffer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#!/usr/bin/env python3
# coding=utf-8
# date 2021-11-13 05:10:10
# author calllivecn <[email protected]>
import io
import sys
import ssl
import hashlib
import time
from typing import (
Union,
Iterable,
)
class Buffer(bytearray):
def __init__(self, size: int = 4096):
super().__init__(size)
# self._ba = bytearray(size)
self._mv = memoryview(self)
def __getitem__(self, slice: slice) -> memoryview:
return self._mv[slice]
def getvalue(self, start: int = 0, end: Union[int, None] = None) -> bytes:
return self._mv[start:end].tobytes()
# data = Buffer(128)
SIZE = 10*(1<<20)
SIZE = (1<<13) # 8k
SIZE = (1<<14) # 16k
# buf = memoryview(bytearray(SIZE))
# 并不能提高性能。。。。
# 看看使用这种方式是不是会省内存。 是省内存,省了差不多一半。 SIZE 为 16k 时,运行速度最快。
if __name__ == "__main__":
buf = Buffer(SIZE)
sha = hashlib.sha256()
with open(sys.argv[1], "rb") as f:
while (n := f.readinto(buf)) != 0:
sha.update(buf[:n])
print(sha.hexdigest())