贴一下我写的程序...
import multiprocessing import time def calc(cpu_number, cpu_total, result, total): sumlimit = 1000 for n in range(cpu_number, total, cpu_total): s1 = 0 s2 = 0 sum = 0 for k in range(0, n): s1 += (pow(16, n-k) % (k * 8 + 1)) / (k * 8 + 1) for k in range(n+1, sumlimit): s2 += (pow(16, n-k)) / (k * 8 + 1) sum += (s1 + s2) * 4 s1 = 0 s2 = 0 for k in range(0, n): s1 += (pow(16, n-k) % (k * 8 + 4)) / (k * 8 + 4) for k in range(n+1, sumlimit): s2 += (pow(16, n-k)) / (k * 8 + 4) sum -= (s1 + s2) * 2 s1 = 0 s2 = 0 for k in range(0, n): s1 += (pow(16, n-k) % (k * 8 + 5)) / (k * 8 + 5) for k in range(n+1, sumlimit): s2 += (pow(16, n-k)) / (k * 8 + 5) sum -= (s1 + s2) s1 = 0 s2 = 0 for k in range(0, n): s1 += (pow(16, n-k) % (k * 8 + 6)) / (k * 8 + 6) for k in range(n+1, sumlimit): s2 += (pow(16, n-k)) / (k * 8 + 6) sum -= (s1 + s2) ret = (sum % 1) * 16 if ret < 0: ret += 16 result[n] = int(ret) def main(): print('Using CPU') startt = time.time() grid = 4 block = 1024 total = grid * block cpu_n = multiprocessing.cpu_count() cpu_result = multiprocessing.Array('d', total) process = [] for i in range(0, cpu_n): print('Starting CPU ' + str(i)) p = multiprocessing.Process(target=calc, args=(i, cpu_n, cpu_result, total)) process.append(p) p.start() for i in process: i.join() f = open('pi_bin_CPU.txt', 'w') for i in range(0, total): f.write((bin(int(cpu_result[i]))).replace('0b','').zfill(4) + ' ') f.close() final = 3.140625 for i in range(2, total): try: final = final + cpu_result[i] / pow(16, i+1) except: break print(final) print('Time elapsed:' + str(time.time() - startt)) if __name__ == "__main__": main()
自学了python一两天,程序写不好见谅
时段 | 个数 |
---|---|
{{f.startingTime}}点 - {{f.endTime}}点 | {{f.fileCount}} |