多线程多进程add测试
import time
from concurrent.futures import ProcessPoolExecutor,ThreadPoolExecutor
def add(num):
a=0
for i in range(num):
a+=1
return a
if __name__=="__main__":
pool_process=ProcessPoolExecutor(10)
x1=[]
t_process=time.time()
for i in range(10):
a=pool_process.submit(add,10000000)
x1.append(a)
x=[i.result() for i in x1]
print(x,"多进程",time.time()-t_process)
x2=[]
t_s=time.time()
for j in range(10):
b=add(10000000)
x2.append(b)
print(x2,"单进程",time.time()-t_s)
p_thread=ThreadPoolExecutor(10)
y=[]
t_y=time.time()
for i in range(10):
a=p_thread.submit(add,10000000)
y.append(a)
z=[k.result() for k in y]
print(z,"多线程",time.time()-t_y)
结论 对于计算密集型结果多进程更快
cache测试
import time
from concurrent.futures import ProcessPoolExecutor,ThreadPoolExecutor
from functools import lru_cache
@lru_cache(128)
def add(num):
a=0
for i in range(num):
a+=1
return a
if __name__=="__main__":
pool_process=ProcessPoolExecutor(10)
x1=[]
t_process=time.time()
for i in range(10):
a=pool_process.submit(add,10000000+i)
x1.append(a)
x=[i.result() for i in x1]
print(x,"多进程",time.time()-t_process)
x2=[]
t_s=time.time()
for j in range(10):
b=add(10000000+j)
x2.append(b)
print(x2,"单进程",time.time()-t_s)
p_thread=ThreadPoolExecutor(10)
y=[]
t_y=time.time()
for i in range(10):
a=p_thread.submit(add,10000000+i)
y.append(a)
z=[k.result() for k in y]
print(z,"多线程",time.time()-t_y)
result:
[10000000, 10000001, 10000002, 10000003, 10000004, 10000005, 10000006, 10000007, 10000008, 10000009] 多进程 0.9088723659515381
[10000000, 10000001, 10000002, 10000003, 10000004, 10000005, 10000006, 10000007, 10000008, 10000009] 单进程 5.408532381057739
[10000000, 10000001, 10000002, 10000003, 10000004, 10000005, 10000006, 10000007, 10000008, 10000009] 多线程 0.001992464065551758
结论:可以看出来使用缓存的话对于多线程的提速是十分明显的
jit测试
import time
from concurrent.futures import ProcessPoolExecutor,ThreadPoolExecutor
from functools import lru_cache
from numba import jit
@lru_cache(128)
@jit(nopython=True)
def add(num):
a=0
for i in range(num):
a+=1
return a
if __name__=="__main__":
pool_process=ProcessPoolExecutor(10)
x1=[]
t_process=time.time()
for i in range(10):
a=pool_process.submit(add,10000000+i)
x1.append(a)
x=[i.result() for i in x1]
print(x,"多进程",time.time()-t_process)
x2=[]
t_s=time.time()
for j in range(10):
b=add(10000000+j)
x2.append(b)
print(x2,"单进程",time.time()-t_s)
p_thread=ThreadPoolExecutor(10)
y=[]
t_y=time.time()
for i in range(10):
a=p_thread.submit(add,10000000+i)
y.append(a)
z=[k.result() for k in y]
print(z,"多线程",time.time()-t_y)
'''
no jit
no cache
[10000000, 10000001, 10000002, 10000003, 10000004, 10000005, 10000006, 10000007, 10000008, 10000009] 多进程 2.01995587348938
[10000000, 10000001, 10000002, 10000003, 10000004, 10000005, 10000006, 10000007, 10000008, 10000009] 单进程 5.405317783355713
[10000000, 10000001, 10000002, 10000003, 10000004, 10000005, 10000006, 10000007, 10000008, 10000009] 多线程 5.500190019607544
'''
'''
only jit
[10000000, 10000001, 10000002, 10000003, 10000004, 10000005, 10000006, 10000007, 10000008, 10000009] 多进程 1.8606822490692139
[10000000, 10000001, 10000002, 10000003, 10000004, 10000005, 10000006, 10000007, 10000008, 10000009] 单进程 0.2473442554473877
[10000000, 10000001, 10000002, 10000003, 10000004, 10000005, 10000006, 10000007, 10000008, 10000009] 多线程 0.004988670349121094
'''
'''
only cache
[10000000, 10000001, 10000002, 10000003, 10000004, 10000005, 10000006, 10000007, 10000008, 10000009] 多进程 2.0952060222625732
[10000000, 10000001, 10000002, 10000003, 10000004, 10000005, 10000006, 10000007, 10000008, 10000009] 单进程 5.454511404037476
[10000000, 10000001, 10000002, 10000003, 10000004, 10000005, 10000006, 10000007, 10000008, 10000009] 多线程 0.003581523895263672
'''
'''
jit and cache
[10000000, 10000001, 10000002, 10000003, 10000004, 10000005, 10000006, 10000007, 10000008, 10000009] 多进程 1.7480416297912598
[10000000, 10000001, 10000002, 10000003, 10000004, 10000005, 10000006, 10000007, 10000008, 10000009] 单进程 0.24353337287902832
[10000000, 10000001, 10000002, 10000003, 10000004, 10000005, 10000006, 10000007, 10000008, 10000009] 多线程 0.002007007598876953
'''
结论: 只要使用加速方案,多线程的速度最快 对于计算密集型不一定是多进程最快
C++单进程:
#include<iostream>
#include<ctime>
using namespace std;
int add(int n);
int main() {
clock_t startTime, endTime;
startTime = clock();//计时开始
int x = 0;
int arr [10] = {};
for (int i = 0; i < 10; i++) {
x = add(10000000);
arr[i] = x;
}
endTime = clock();//计时结束
for (int i = 0; i < 10; i++) {
cout << arr[i] << endl;
}
cout << "The run time is: " << (double)(endTime - startTime) / CLOCKS_PER_SEC << "s" << endl;
return 0;
}
int add(int n) {
int b = 0;
for (int i = 0; i < n; i++) {
b += 1;
}
return b;
}
C++多线程:
#include<iostream>
#include<ctime>
#include<windows.h>
#include<process.h>
using namespace std;
unsigned int __stdcall add(PVOID pM);
int main() {
clock_t startTime, endTime;
startTime = clock();//计时开始
const int THREAD_NUM = 10;
HANDLE handle[THREAD_NUM];
int x = 0;
int arr [10] = {};
for (int i = 0; i < 10; i++) {
handle [i]=(HANDLE)_beginthreadex(NULL, 0, add, 0, 0, NULL);
}
DWORD exitcode = 0;
for (int i = 0; i < 10; i++) {
WaitForSingleObject(handle[i], INFINITE);
GetExitCodeThread(handle[i], & exitcode);
arr[i] = exitcode;
}
endTime = clock();//计时结束
for (int i = 0; i < 10; i++) {
cout << arr[i] << endl;
}
cout << "The run time is: " << (double)(endTime - startTime) / CLOCKS_PER_SEC << "s" << endl;
return 0;
}
unsigned int __stdcall add(PVOID pM) {
int n = 10000000;
int b = 0;
for (int i = 0; i < n; i++) {
b += 1;
}
return b;
}
多线程多进程read测试
import time
from concurrent.futures import ProcessPoolExecutor,ThreadPoolExecutor
from functools import lru_cache
from numba import jit
import cv2
def read(num):
for i in range(num):
bgr = cv2.imread("1.jpg")
return bgr
if __name__=="__main__":
pool_process=ProcessPoolExecutor(10)
x1=[]
t_process=time.time()
for i in range(10):
a=pool_process.submit(read,100+i)
x1.append(a)
x=[i.result() for i in x1]
print("多进程",time.time()-t_process)
x2=[]
t_s=time.time()
for j in range(10):
b=read(100+j)
x2.append(b)
print("单进程",time.time()-t_s)
p_thread=ThreadPoolExecutor(10)
y=[]
t_y=time.time()
for i in range(10):
a=p_thread.submit(read,100+i)
y.append(a)
z=[k.result() for k in y]
print("多线程",time.time()-t_y)
'''
cache
多进程 2.3875579833984375
单进程 6.432114839553833
多线程 0.002981901168823242
'''
'''
no cache
多进程 2.349823474884033
单进程 6.468693017959595
多线程 1.166515588760376
'''
|