Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Efficiency Analysis of Quick sorting on dual-Core CPU

2025-04-12 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "Quick sort efficiency Analysis on dual-core CPU". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "Quick sorting efficiency Analysis on dual-core CPU".

In order to test the efficiency of sorting algorithm on multi-core CPU, we have to compare the gap between single-task sorting algorithm and multi-task parallel sorting algorithm, so we choose fast sorting algorithm to compare.

Test environment: dual-core CPU 2.66GHZ

Mononuclear CPU 2.4GHZ

The following is the source code for a quick sort algorithm:

UINTSplit (void * ppData, UINTuStart, UINTuEnd, COMPAREFUNCCompareFunc) {void * pSelData; UINTuLow; UINTuHigh; uLow = uStart; uHigh = uEnd; pSelData = ppData [uLow]; while (uLow

< uHigh ) { while ( (*CompareFunc)(ppData[uHigh], pSelData) >

0 & & uLow! = uHigh) {--uHigh;} if (uHigh! = uLow) {ppData [uLow] = ppData [uHigh]; + + uLow;} while ((* CompareFunc) (ppData [uLow], pSelData)

< 0 && uLow != uHigh ) { ++uLow; } if ( uLow != uHigh ) { ppData[uHigh] = ppData[uLow]; --uHigh; } } ppData[uLow] = pSelData; returnuLow; } voidQuickSort(void **ppData, UINTuStart, UINTuEnd, COMPAREFUNCCompareFunc) { UINTuMid = Split(ppData, uStart, uEnd, CompareFunc ); if ( uMid >

UStart) {QuickSort (ppData, uStart, uMid-1, CompareFunc);} if (uEnd > uMid) {QuickSort (ppData, uMid + 1, uEnd, CompareFunc);}}

First test the time it takes for this quick sorting algorithm to rank a million random integers:

VoidTest_QuickSort (void) {UINTi; UINTuCount = 1000000; / / 1000000 srand (time (NULL)); void * * pp = (void * *) malloc (uCount * sizeof (void *)); for (I = 0; I

< uCount; i++ ) { pp[i] = (void *)(rand() % uCount); } clockclock_tt1 = clock(); QuickSort(pp, 0, uCount-1, UIntCompare); clockclock_tt2 = clock(); printf("QuickSort 1000000 Time %ld/n", t2-t1); free(pp); } 在双核CPU2.66GHZ机器上运行测试程序,打印出花费的时间约为406 ms 在单核CPU2.4GHZ机器上运行测试程序,打印出花费时间约为484ms 可见在双核CPU上运行单任务程序和单核CPU基本是一样的,效率没有任何提高。 下面再来把上面的快速排序程序变成并行的,一个简单的方法就是将要排序的区间分成相同的几个段,然后对每个段进行快速排序,排序完后再使用归并算法将排好的几个区间归并成一个排好序的表,我们先四个线程来进行排序,代码如下: void ** Merge(void **ppData, UINTuStart, UINTuEnd, void **ppData2, UINTuStart2, UINTuEnd2, COMPAREFUNCcfunc) { UINTi, j, k; UINTu1, u2, v1,v2; void **pp1; void **pp2; void **pp = (void **)malloc( (uEnd-uStart+1+uEnd2-uStart2+1) * sizeof(void *)); if ( pp == NULL ) { returnNULL; } if ( (*cfunc)(ppData2[uStart2], ppData[uStart]) >

0) {U1 = uStart; U2 = uEnd; v1 = uStart2; v2 = uEnd2; pp1 = ppData; pp2 = ppData2;} else {U1 = uStart2; U2 = uEnd2; v1 = uStart; v2 = uEnd; pp1 = ppData2; pp2 = ppData;} k = 0 Pp [k] = pp1 [U1]; j = v1; for (I = U1N1; I uEnd, pNode- > func); return 1;} # define THREAD_COUNT 4 INTMQuickSort (void * * ppData, UINTuStart, UINTuEnd, COMPAREFUNCCompareFunc) {void * * pp1; void * * pp2; void * * pp3; INT i; SORTNODE Node [thread _ COUNT]; HANDLE hthread [thread _ COUNT] INT nRet = CAPI_FAILED; for (I = 0; I < THREAD_COUNT; iTunes +) {Node [I] .ppData = ppData; if (I = = 0) {Node [I] .uStart = uStart } else {Node [I] .uStart = uEnd * I / THREAD_COUNT + 1;} Node [I] .uEnd = uEnd * (iNode 1) / THREAD_COUNT; Node [I] .func = CompareFunc HThread [I] = CreateThread (NULL, 0, QuickSort_Thread, & (Node [I]), 0, NULL);} for (I = 0; I < THREAD_COUNT; iTunes +) {WaitForSingleObject (hThread [I], INFINITE);} pp1 = Merge (ppData, uStart, uEnd/4, ppData, uEnd/4+1, uEnd/2, CompareFunc) Pp2 = Merge (ppData, uEnd/2+1, uEnd*3/4, ppData, uEnd*3/4+1, uEnd, CompareFunc); if (pp1! = NULL & & pp2! = NULL) {pp3 = Merge (pp1, 0, uEnd/2-uStart, pp2, 0, uEnd-uEnd/2- 1, CompareFunc); if (pp3! = NULL) {UINTi; for (I = uStart; I

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report