C 多线程与单线程进程运行时
我目前正在开发一个多线程合并排序程序。 我正在比较多线程和单线程的不同运行时间。 我得出的平均值是0.022279秒(多个)与0.00249秒(单个) 我的问题是为什么单线程应用程序比我的多线程应用程序快 多线程不应该比单线程更高效吗 多线程代码C 多线程与单线程进程运行时,c,multithreading,operating-system,C,Multithreading,Operating System,我目前正在开发一个多线程合并排序程序。 我正在比较多线程和单线程的不同运行时间。 我得出的平均值是0.022279秒(多个)与0.00249秒(单个) 我的问题是为什么单线程应用程序比我的多线程应用程序快 多线程不应该比单线程更高效吗 多线程代码 #include <pthread.h> #include <stdlib.h> #include <string.h> #include <stdio.h> void *run(void *par
#include <pthread.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
void *run(void *parameters); /* threads call this function */
int alength, flength;
int *array_whole;
FILE *input_file;
int *read_file(char *file_name) {
input_file = fopen("soulfoodinput.txt", "rt"); // open file
int arraysize = flength;
char line[80];
int integer;
int index = 0;
int *input = malloc(arraysize * sizeof(int));
while (fgets(line, 80, input_file) != NULL)
{
sscanf(line, "%d", &integer); // read the integer value
input[index] = integer;
//printf(line);
++index;
++alength;
}
fclose(input_file); // close the file
return input;
}
int read_length(char *file_name) {
input_file = fopen(file_name, "rt"); // open file
char line[80];
int file_length = 0;
while (fgets(line, 80, input_file) != NULL) {
file_length += 1;
}
return file_length;
}
void merge(int arr[], int left, int middle, int right) //function to sort unsorted array
{
int i, j, k;
//create array partition
int left_partition = middle - left + 1;
int right_partition = right - middle;
int first[left_partition], second[right_partition]; //set up temporary arrays
//move left side of array to temp array
for (i = 0; i < left_partition; i++)
{
first[i] = arr[left + i];
}
//move right side of array to temp array
for (j = 0; j < right_partition; j++)
{
second[j] = arr[middle + 1 + j];
}
i = 0;
j = 0;
k = left;
while (i < left_partition && j < right_partition)
{
//sort the array into one array
if (first[i] <= second[j])
{
arr[k] = first[i];
++i;
}
else
{
arr[k] = second[j];
j++;
}
k++;
}
while (i < left_partition)
{
arr[k] = first[i];
i++;
k++;
}
while (j < right_partition)
{
arr[k] = second[j];
j++;
k++;
}
}
void merge_sort(int arr[], int left, int right) //splits array in half, calls self on both halves, then merges the halves
{
if (left < right)
{
int middle = (left + (right-1))/ 2;
merge_sort(arr, left, middle); //split left half
merge_sort(arr, middle+1, right); //split right half
merge(arr, left, middle, right); //merge both halves
}
}
int main(int argc, char *argv[])
{
/*WILL CREATES THREAD ID 1, 2, 3 */
pthread_t thread_id1; // creates first thread id
pthread_attr_t attr_1; // creates thread attributes
pthread_t thread_id2; // creates second thread id
pthread_attr_t attr_2; // second thread attributes
pthread_t thread_id3; // creates third thread id
pthread_attr_t attr_3;// third thread attributes
/* WILL READ IN FILE WITH UNSORTED ARRAY*/
char *file_name = argv[1];
flength = read_length(file_name);
array_whole = read_file(file_name);
clock_t t;
int m;
printf("UNSORTED: ");
for (m = 0; m < alength; m++) {
if (m == alength - 1) {
printf("%d \n", array_whole[m]);
}
else {
printf("%d, ", array_whole[m]);
}
}
t=clock();
/*WILL CREATE THREAD 1,2,3 */
char *thread_1 = "first"; // creates first thread
pthread_attr_init(&attr_1); //finds attributes
pthread_create(&thread_id1, &attr_1, run, thread_1); // create 1st thread
char *thread_2 = "second";
pthread_attr_init(&attr_2);
pthread_create(&thread_id2, &attr_2, run, thread_2); // create 2nd thread
char *thread_3 = "third";
pthread_attr_init(&attr_3);
pthread_create(&thread_id3, &attr_2, run, thread_3); // create 3rd thread
/*WILL JOIN ALL THE THREADS TOGETHER*/
pthread_join(thread_id1, NULL);
pthread_join(thread_id2, NULL);
pthread_join(thread_id3, NULL);
t=clock() - t;
double time_taken = ((double)t)/CLOCKS_PER_SEC;
/* WILL PRINT ALL SORTED VALUES */
int i;
printf("SORTED: ");
for (i = 0; i < alength; i++) {
if (i == alength - 1) {
printf("%d \n", array_whole[i]);
}
else {
printf("%d, ", array_whole[i]);
}
}
printf("elapsed time = %f seconds to execute \n", time_taken);
pthread_exit(0);
return 0;
}
void *run(void *parameters)
{
int centerspot = alength / 2;
if (strcmp(parameters, "first") == 0) {
merge_sort(array_whole, 0, centerspot);
}
if (strcmp(parameters, "second") == 0) {
merge_sort(array_whole, centerspot + 1, alength - 1);
}
if (strcmp(parameters, "third") == 0) {
merge_sort(array_whole, 0, alength - 1);
}
pthread_exit(0);
}
#include <pthread.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
void *run(void *parameters); /* threads call this function */
int alength, flength;
int *array_whole;
FILE *input_file;
int *read_file(char *file_name) {
input_file = fopen("soulfoodinput.txt", "rt"); // open file
int arraysize = flength;
char line[80];
int integer;
int index = 0;
int *input = malloc(arraysize * sizeof(int));
while (fgets(line, 80, input_file) != NULL)
{
sscanf(line, "%d", &integer); // read the integer value
input[index] = integer;
//printf(line);
++index;
++alength;
}
fclose(input_file); // close the file
return input;
}
int read_length(char *file_name) {
input_file = fopen(file_name, "rt"); // open file
char line[80];
int file_length = 0;
while (fgets(line, 80, input_file) != NULL) {
file_length += 1;
}
return file_length;
}
void merge(int arr[], int left, int middle, int right) //function to sort unsorted array
{
int i, j, k;
//create array partition
int left_partition = middle - left + 1;
int right_partition = right - middle;
int first[left_partition], second[right_partition]; //set up temporary arrays
//move left side of array to temp array
for (i = 0; i < left_partition; i++)
{
first[i] = arr[left + i];
}
//move right side of array to temp array
for (j = 0; j < right_partition; j++)
{
second[j] = arr[middle + 1 + j];
}
i = 0;
j = 0;
k = left;
while (i < left_partition && j < right_partition)
{
//sort the array into one array
if (first[i] <= second[j])
{
arr[k] = first[i];
++i;
}
else
{
arr[k] = second[j];
j++;
}
k++;
}
while (i < left_partition)
{
arr[k] = first[i];
i++;
k++;
}
while (j < right_partition)
{
arr[k] = second[j];
j++;
k++;
}
}
void merge_sort(int arr[], int left, int right) //splits array in half, calls self on both halves, then merges the halves
{
if (left < right)
{
int middle = (left + (right-1))/ 2;
merge_sort(arr, left, middle); //split left half
merge_sort(arr, middle+1, right); //split right half
merge(arr, left, middle, right); //merge both halves
}
}
int main(int argc, char *argv[])
{
/*WILL CREATES THREAD ID */
pthread_t thread_id1; // creates first thread id
pthread_attr_t attr_1; // creates thread attributes
/* WILL READ IN FILE WITH UNSORTED ARRAY*/
char *file_name = argv[1];
flength = read_length(file_name);
array_whole = read_file(file_name);
clock_t t;
int m;
printf("UNSORTED: ");
for (m = 0; m < alength; m++) {
if (m == alength - 1) {
printf("%d \n", array_whole[m]);
}
else {
printf("%d, ", array_whole[m]);
}
}
t=clock();
/*WILL CREATE THREAD 1,2,3 */
char *thread_1 = "first"; // creates first thread
pthread_attr_init(&attr_1); //finds attributes
pthread_create(&thread_id1, &attr_1, run, thread_1); // create 1st thread
/*WILL JOIN ALL THE THREADS TOGETHER*/
pthread_join(thread_id1, NULL);
t=clock() - t;
double time_taken = ((double)t)/CLOCKS_PER_SEC;
/* WILL PRINT ALL SORTED VALUES */
int i;
printf("SORTED: ");
for (i = 0; i < alength; i++) {
if (i == alength - 1) {
printf("%d \n", array_whole[i]);
}
else {
printf("%d, ", array_whole[i]);
}
}
printf("elapsed time = %f seconds to execute \n", time_taken);
pthread_exit(0);
return 0;
}
void *run(void *parameters)
{
int centerspot = alength / 2;
merge_sort(array_whole, 0, centerspot);
merge_sort(array_whole, centerspot + 1, alength - 1);
merge_sort(array_whole, 0, alength - 1);
pthread_exit(0);
}
#包括
#包括
#包括
#包括
void*运行(void*参数);/*线程调用这个函数*/
内部长度,长度;
整型数组;
文件*输入文件;
int*读取文件(char*文件名){
input_file=fopen(“soulfoodinput.txt”,“rt”);//打开文件
int arraysize=flength;
字符行[80];
整数;
int指数=0;
int*input=malloc(arraysize*sizeof(int));
while(fgets(第80行,输入文件)!=NULL)
{
sscanf(第%d行,&integer);//读取整数值
输入[索引]=整数;
//printf(行);
++指数;
++阿伦特;
}
fclose(输入文件);//关闭文件
返回输入;
}
整数读取长度(字符*文件名){
input_file=fopen(文件名,“rt”);//打开文件
字符行[80];
int file_length=0;
while(fgets(第80行,输入文件)!=NULL){
文件长度+=1;
}
返回文件长度;
}
void merge(int arr[],int left,int middle,int right)//对未排序的数组进行排序的函数
{
int i,j,k;
//创建阵列分区
int left_partition=middle-left+1;
int right_partition=右-中;
int第一个[left_partition],第二个[right_partition];//设置临时数组
//将阵列左侧移动到临时阵列
对于(i=0;i<左分区;i++)
{
第一[i]=arr[左+i];
}
//将阵列的右侧移动到临时阵列
对于(j=0;j 如果(第一个[i]线程需要时间来设置和启动。要完成的工作量越大,多个线程减少完成任务所需总时间的可能性就越大
在您的示例中,我猜soulfoodinput.txt文件不是很大,因此我建议将其放大,如果可能的话,它应该包含数百甚至数千行。然后查看多线程代码是否比单线程运行得更快。线程设置和启动需要时间。要完成的工作量越大,任务越复杂可能多个线程会减少完成任务所需的总时间
在您的示例中,我猜soulfoodinput.txt文件不是很大,所以我建议将其放大,如果可能的话,它应该包含数百甚至数千行。然后看看多线程代码是否比单线程运行得更快。多线程的性能可以用大量数据来衡量。数据量非常小数据量无法衡量多线程应用程序的性能。原因如下:-
1.要创建线程,O/S需要为每个线程分配内存,这需要时间(即使是很小一点)
2.创建多线程时,需要切换上下文,这也需要时间
3.需要释放分配给线程的内存,这也需要时间
4.这取决于您机器中的处理器数量和总内存(RAM)
因此,当您尝试使用多线程的小操作时,它的性能将与单线程相同,甚至更低。因此,在这种情况下,您的结果是完美的。要测量多线程体系结构的性能,请使用大量数据和复杂的操作,然后只有您才能看到差异。多线程的性能可以使用大量数据进行测量。使用非常小的数据量,您无法测量多线程应用程序的性能。原因如下:-
1.要创建线程,O/S需要为每个线程分配内存,这需要时间(即使是很小一点)
2.创建多线程时,需要切换上下文,这也需要时间
3.需要释放分配给线程的内存,这也需要时间
4.这取决于您机器中的处理器数量和总内存(RAM)
因此,当您尝试使用多线程的小操作时,它的性能将与单线程相同,甚至更低。因此,在这种情况下,您的结果是完美的。要测量多线程体系结构的性能,请使用具有复杂操作的大量数据,这样只有您才能看到差异。请正确缩进您的代码(这是不可读的)尽量把它简化为一个最小的例子。为了清楚起见,你有一个线程对上半部分进行排序,另一个线程对下半部分进行排序。在这过程中,第三个线程在做什么?我根本不知道这里发生了什么。上一次合并不应该等到两个部分排序后进行吗?@MartinJames第三个线程将两个线程合并在一起第一个线程将数组的左侧分割并排序,第二个线程将数组的右侧分割并排序请正确缩进您的代码(这是不可读的)尽量把它简化为一个最小的例子。为了清楚起见,你有一个线程对上半部分进行排序,另一个线程对下半部分进行排序。在这过程中,第三个线程在做什么?我根本不知道这里发生了什么。上一次合并不应该等到两个部分排序后进行吗?@MartinJames第三个线程将两个线程合并在一起ads在一起。第一个线程对数组的左侧进行分区并对其进行排序,第二个线程对数组的右侧进行分区并对其进行排序,即使使用了更大的数据集。在这种情况下,1000个整数的数组与不大的多线程相比,单线程的计算时间更快