單線程與多線程排序對比,單線程多線程排序
單線程排序
【快速排序,使用STL sort函數】
#include <stdio.h>#include <stdlib.h>#include <pthread.h>#include <sys/time.h>#include <algorithm>using namespace std;#define NUMNUM 8000000Llong nums[NUMNUM]; //待排序數組bool compare(long a, long b){ return a < b;}int main(){ freopen("back.txt","w",stdout); unsigned long i; struct timeval start, end; long long startusec, endusec; double elapsed; srandom(time(NULL)); for (i = 0; i < NUMNUM; i++) nums[i] = random(); //計時開始 gettimeofday(&start,NULL); sort(nums,nums+NUMNUM,compare); //單線程排序,快速排序 gettimeofday(&end,NULL); //計算用時 startusec = start.tv_sec * 1000000 + start.tv_usec; endusec = end.tv_sec * 1000000 + end.tv_usec; elapsed = (double)(endusec - startusec) / 1000000.0; printf("sort took %.4f seconds\n", elapsed); //查看是否已經排好序 for (i = 0; i < NUMNUM; i++) printf("%ld\n", nums[i]); exit(0);}
耗時:
1、單核,單線程
sort took 2.2911 seconds
2、雙核,四線程
sort took 3.1645 seconds
sort took 3.1613 seconds
多線程排序
#include <stdio.h>#include <stdlib.h>#include <limits.h>#include <pthread.h>#include <sys/time.h>#include <algorithm>using namespace std;#define NTHR 8 /* number of threads */#define NUMNUM 8000000L /* number of numbers to sort */#define TNUM (NUMNUM/NTHR) /* number to sort per thread */long nums[NUMNUM];long snums[NUMNUM];pthread_barrier_t b; //屏障bool compare(long a, long b){ return a < b;}/* * Worker thread to sort a portion of the set of numbers. */void *workThread(void *arg){ long idx = (long)arg; sort(&nums[idx],&nums[idx+TNUM],compare); pthread_barrier_wait(&b); pthread_exit(NULL);}/* * Merge the results of the individual sorted ranges. */void merge(){ long idx[NTHR]; long i, minidx, sidx, num; for (i = 0; i < NTHR; i++) idx[i] = i * TNUM; for (sidx = 0; sidx < NUMNUM; sidx++) { num = LONG_MAX; for (i = 0; i < NTHR; i++) { if ((idx[i] < (i+1)*TNUM) && (nums[idx[i]] < num)) { num = nums[idx[i]]; minidx = i; } } snums[sidx] = nums[idx[minidx]]; idx[minidx]++; }}int main(){ freopen("back.txt","w",stdout); unsigned long i; struct timeval start, end; long long startusec, endusec; double elapsed; int err; pthread_t tid; /* * Create the initial set of numbers to sort. */ srandom(time(NULL)); for (i = 0; i < NUMNUM; i++) nums[i] = random(); /* * 建立八個線程分別對數組的八個段進行排序 */ gettimeofday(&start, NULL); pthread_barrier_init(&b, NULL, NTHR+1); for (i = 0; i < NTHR; i++) { err = pthread_create(&tid, NULL,workThread, (void *)(i * TNUM)); if (err != 0) { perror("pthread_create"); return -1; } } pthread_barrier_wait(&b); merge(); gettimeofday(&end, NULL); //計算用時 startusec = start.tv_sec * 1000000 + start.tv_usec; endusec = end.tv_sec * 1000000 + end.tv_usec; elapsed = (double)(endusec - startusec) / 1000000.0; printf("sort took %.4f seconds\n", elapsed); for (i = 0; i < NUMNUM; i++) printf("%ld\n", snums[i]); exit(0);}
耗時
1、單核,單線程
sort took 2.5428 seconds
2、雙核,四線程CPU
8個線程
sort took 1.4124 seconds
sort took 1.3863 seconds
6線程
sort took 1.3010 seconds
Sort took 1.2956 seconds
4個線程
sort took 1.2626 seconds
sort took 1.2686 seconds
2個線程
sort took 2.2026 seconds
CPU資訊
總結:
可以看到儘管在分別進行排序之後還要有合并(merge)這一步,多線程排序還是要優於單線程排序(在CPU為多核的情況下),而且在CPU為四線程下,使用四個線程對數組進行排序,所耗費時間是最少的!
附:Makefile源碼
CC = g++
CPPFLAGS = -Wall -g -pthread
BIN = main
SOURCES = $(wildcard *.cpp)
OBJECTS = $(SOURCES:.cpp=.o)
.PHONY: clean all
all: $(BIN)
$(BIN): $(OBJECTS)
$(CC) $(CPPFLAGS) -o $@ $<
%.o: %.cpp
$(CC) $(CPPFLAGS) -o $@ -c $<
clean:
-rm -rf $(BIN) $(OBJECTS)