Comparing single-thread and multi-thread sorting, single-thread multi-thread sorting
Single-thread sorting
[Quick sorting, using the STL sort function]
# Include <stdio. h> # include <stdlib. h> # include <pthread. h> # include <sys/time. h >#include <algorithm> using namespace std; # define NUMNUM 8000000 Llong 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 startusec, endusec; double elapsed; srandom (time (NULL); for (I = 0; I <NUMNUM; I ++) nums [I] = random (); // start time gettimeofday (& start, NULL); sort (nums, nums + NUMNUM, compare); // sort by single thread, and quickly sort gettimeofday (& end, NULL ); // when computing, 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); // check whether the order for (I = 0; I <NUMNUM; I ++) printf (" % ld \ n ", nums [I]); exit (0 );}
Time consumed:
1. Single-core, single-Thread
Sort took 2.2911 seconds
2. dual-core, four threads
Sort took 3.1645 seconds
Sort took 3.1613 seconds
Multi-thread sorting
# 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 8001_l/* 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;}/** Wo Rker 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 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 (); /** create eight threads to sort the eight segments of the array */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 ); // when computing, 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 );}
Time consumed
1. Single-core, single-Thread
Sort took 2.5428 seconds
2. dual-core, four-thread CPU
8 threads
Sort took 1.4124 seconds
Sort took 1.3863 seconds
6 threads
Sort took 1.3010 seconds
Sort took 1.2956 seconds
4 threads
Sort took 1.2626 seconds
Sort took 1.2686 seconds
2 threads
Sort took 2.2026 seconds
CPU Information
Summary:
We can see that, even after sorting separately, there is also a merge step. multi-threaded sorting is better than single-threaded sorting (when the CPU is multi-core ), in addition, when the CPU is four threads and four threads are used to sort the array, the time consumed is the least!
Appendix: Makefile source code
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)