Time sharing issues

I tried to see the effect of time lapses. And how can it consume a significant amount of time. In fact, I tried to divide a certain work by the number of threads and see the effect.

I have a dual core processor. Thus, two threads can work in parallel. I tried to check if I have w work that is performed by two threads, and if I have the same work as t-threads, with every thread doing w / t work. How long does it take?

Since slicing time is a time consuming process, I expected that when I do the same work using a process with two threads or using a t-thread process, the time taken by the t-thread process will be longer

However, I found that this is not the case. I tried with t = 10. And yet it is faster than a process with two threads. E.g. if I need to do 10,000,000 iterations, then with two thread processes, I will have 2 threads, iterations per 5,000,000, so we have a total of 10,000,000 iterations. If I am connected to a process with 10 threads, I will give each thread an iteration of 1,000,000 so that we have a total of 10,000,000.

I expected a process with 10 threads to take longer. But this is not so. Is there a mistake in the code? I'm fine.

Any suggestions?

+5
source share
4 answers

10000000 (10 ) x 1000 5000000 (5 ) x 1000 . , , . .

2 2 ( , 2 ), , .

, , , . , :

./a.out 2 500000000
= 2
= 250000000
- = 5.931148

./a.out 1000 500000000
= 1000
= 500000
= 6,563666

./a.out 2000 500000000
= 2000
= 250000
, = 7.087449

. :

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

long* array;
int length;
int threads;

void *tfunc(void *arg) {
  int n = (int)arg;
  int i;
  int j;
  int x;
  long sum = 0;
  //printf("%d\n",*n);
  int start = n * (length / threads);
  int end = (n + 1) * (length / threads);

  for (i=start; i<end; i++) {
    array[i] = array[i] * array[i];
    //printf("%d\n",i);
  }
  return(0);

}

double timestamp() {
  struct timeval tp;
  gettimeofday(&tp, NULL);
  return (double)tp.tv_sec + tp.tv_usec / 1000000.;
}

int main(int argc, char *argv[]) {
  int numberOfIterations = atoi(argv[2]);
  int numberOfThreads = atoi(argv[1]);
  int i;
  printf("Number of threads = %d\n",numberOfThreads);
  printf("Number of iterations in each thread = %d \n", numberOfIterations / numberOfThreads);
  pthread_t workerThreads[numberOfThreads];
  int *arg = &numberOfIterations;

  array = (long*)malloc(numberOfIterations * sizeof(long));
  length = numberOfIterations;
  threads = numberOfThreads;
  int result[numberOfThreads];

  double timeTaken;
  timeTaken = timestamp();

  for(i=0; i<numberOfThreads; i++) {
    result[i] = pthread_create(workerThreads+i, NULL, tfunc, (void*)i);
  }

  for(i=0; i<numberOfThreads; i++) {
    pthread_join(workerThreads[i], NULL);
  }

  timeTaken = timestamp() - timeTaken;
  printf("Total time taken = %f\n", timeTaken);
  /*printf("The results are\n");
  for(i=0; i<numberOfThreads; i++) {
    printf("%d\n",result[i]);
  }*/

  free(array);
  exit(0);
}
+1

, , , :

1) , .. / . ( , ), , .

2) , , L1 . , , ( , ), .

Windows, , i7, 4/8 :

8 tests,
400 tasks,
counting to 10000000,
using 8 threads:
Ticks: 2199
Ticks: 2184
Ticks: 2215
Ticks: 2153
Ticks: 2200
Ticks: 2215
Ticks: 2200
Ticks: 2230
Average: 2199 ms

8 tests,
400 tasks,
counting to 10000000,
using 32 threads:
Ticks: 2137
Ticks: 2121
Ticks: 2153
Ticks: 2138
Ticks: 2137
Ticks: 2121
Ticks: 2153
Ticks: 2137
Average: 2137 ms

8 tests,
400 tasks,
counting to 10000000,
using 128 threads:
Ticks: 2168
Ticks: 2106
Ticks: 2184
Ticks: 2106
Ticks: 2137
Ticks: 2122
Ticks: 2106
Ticks: 2137
Average: 2133 ms

8 tests,
400 tasks,
counting to 10000000,
using 400 threads:
Ticks: 2137
Ticks: 2153
Ticks: 2059
Ticks: 2153
Ticks: 2168
Ticks: 2122
Ticks: 2168
Ticks: 2138
Average: 2137 ms
+1

? , , , , ( , ), - , .

0

, .

, .

0

All Articles