CHANNELS = 16 SAMPLE_LENGTH = CHANNELS // 8 BYTES_PER_SAMPLE = CHANNELS // SAMPLE_LENGTH BUFFER = 16386 PROCNUM = 4 vals = {} for i in range(2**16): vals[struct.pack('H', i)] = tuple(b'1' if e == '1' else b'0' for e in list("{:016b}".format(i))) with open('largefile.dat', 'rb') as f: data = f.read() size = len(data) print("Data size: %d" % size) out_file = open('largefile_dump2.dat', 'wb') out_list = [out_file]*CHANNELS samples = [] for i in range(0, size // 8, SAMPLE_LENGTH): # if i % (1024**2) == 0: print("i = %d" % i) samples.append(vals[data[i:i+2]]) if len(samples) >= BUFFER: samples = np.matrix(samples).transpose().tolist() # comment if no numpy for channel in range(CHANNELS): s = b''.join(samples[channel]) # comment if no numpy # s = b''.join(r[channel] for r in samples) # uncomment if no numpy, don't forget to change the buffer to 256, otherwise slooow. out_list[channel].write(s) samples = [] if samples: for channel in range(CHANNELS): s = b'\n'.join(r[channel] for r in samples) out_list[channel].write(s) out_file.close()