python核心编程学习笔记-2016-08-28-01-习题18-4和习题18-9
2016-08-28 14:39
1291 查看
习题18-4
但是看结果
习题18-9
单线程比多线程还要快。这个可能是由于这两个程序都是计算密集型的原因吧。具体原因暂时存疑吧,等到具备操作系统的基本知识后,或许就会明白了。
#-*-coding: utf-8-*- import threading import time char = chr(input("Enter a positive number from 0 to 255: ")) filename = raw_input("Enter a filename: ") print "*** Single Thread" count = 0 time_start = time.clock() with open(filename, 'r') as f: for eachLine in f: if eachLine == bin(ord(char)) + '\n': count += 1 f.close() time_end = time.clock() print "%s occur %d time in %s." % (char, count, filename) print "using", (time_end - time_start), 's' print "*** Multiple Threads" counts = [] threadNum = int(raw_input("Enter the number of threads: ")) def read(char, text): count = 0 for eachLine in text: if eachLine == bin(ord(char)) + '\n': count += 1 counts.append(count) time_start = time.clock() with open(filename, 'r') as f: text = f.readlines() lines = len(text) / threadNum threads = [] for i in range(threadNum): if (i + 1) * (len(text) / threadNum) <= len(text): t = threading.Thread(target=read, args=(char, text[(i * lines):((i + 1) * lines)])) # 将文件分块,不同线程分别读取。 else: t = threading.Thread(target=read, args=(char, text[(i * lines):])) threads.append(t) threads[i].start() for i in range(threadNum): threads[i].join() f.close() count = sum(counts) time_end = time.clock() print "%s occur %d time in %s." % (char, count, filename) print "using", (time_end - time_start), 's'
但是看结果
习题18-9
#-*-coding: utf-8-*- import threading import time filenames = ['ex9-19.txt', 'ex9-19.txt', 'ex9-19.txt', 'ex9-19.txt', 'ex9-19.txt'] sin_counts = [] mul_counts = [] def countline(filename, counts): f = open(filename, 'r') count = 0 for eachLine in f: count += 1 counts.append(count) print "*** Single thread" time_start = time.clock() for i in range(len(filenames)): countline(filenames[i], sin_counts) time_end = time.clock() print sin_counts print time_end - time_start print "*** Multiply threads" time_start = time.clock() threads = [] for i in range(len(filenames)): t = threading.Thread(target=countline, args=(filenames[i], mul_counts)) threads.append(t) for i in range(len(filenames)): threads[i].start() for i in range(len(filenames)): threads[i].join() time_end = time.clock() print mul_counts print time_end - time_start结果
单线程比多线程还要快。这个可能是由于这两个程序都是计算密集型的原因吧。具体原因暂时存疑吧,等到具备操作系统的基本知识后,或许就会明白了。
相关文章推荐
- python核心编程学习笔记-2016-08-05-01-习题9-19
- python核心编程学习笔记-2016-08-03-01-习题9-9
- python核心编程学习笔记-2016-09-18-01-数据库编程(三)
- 学习python核心编程-第三章-课后习题笔记
- python核心编程学习笔记-2016-09-25-01-python扩展
- python核心编程学习笔记-2016-08-15-01-左加法__add__和右加法__radd__
- python核心编程学习笔记-2016-09-24-01-数据库编程(四)
- python核心编程学习笔记-2016-08-02-01-读取文件的函数中的文件指针问题
- python学习--核心编程5习题以及知识点记录
- Python核心编程--学习笔记--1--Python简介
- python核心编程学习笔记-2016-07-23-02-切片索引
- Python核心编程--学习笔记--4--Python对象
- Python核心编程--学习笔记--5--数字
- python核心编程学习笔记-2016-08-13-05-property()函数
- Python-核心编程-学习笔记
- [PYTHON] 核心编程笔记(18.多线程编程)
- python 核心编程学习笔记(第3章) 对应Let's-python视频第4集
- Python核心编程--学习笔记--2--Python起步(下)
- python 核心编程学习笔记(1, 2章) 对应Let's-python视频第1, 2, 3集
- Python核心编程--学习笔记--7--字典和集合