码迷,mamicode.com
首页 > 编程语言 > 详细

单线程与多线程排序对比

时间:2014-11-13 18:58:09      阅读:472      评论:0      收藏:0      [点我收藏+]

标签:cc++   linux   排序算法   多线程   

单线程排序

           【快速排序,使用STL sort函数】


#include <stdio.h>
#include <stdlib.h>

#include <pthread.h>
#include <sys/time.h>

#include <algorithm>
using namespace std;

#define NUMNUM 8000000L
long 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信息

 bubuko.com,布布扣

 

 

总结:

     可以看到尽管在分别进行排序之后还要有合并(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)





    

单线程与多线程排序对比

标签:cc++   linux   排序算法   多线程   

原文地址:http://blog.csdn.net/zjf280441589/article/details/41083199

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!