1

Now, the code takes 866678 clock ticks when the code is run in multithread and when I comment the for loops in the threads(each FOR loop runs 10000 times) and just run the whole FOR loop (20000 times). The run time is same for both with and without threads. But ideally it should have been half right?

// thread example
#include <iostream>       // std::cout
#include <thread>         // std::thread
#include <time.h>
#include<cmath>
#include<unistd.h>
int K = 20000;
long int a[20000];

void makeArray(){
    for(int i=0;i<K;i++){
    a[i] = i;
    }
}

void foo()
{
  // do stuff...
  std::cout << "FOOOO Running...\n";
  for(int i=K/2;i<K;i++){
//    a[i] = a[i]*a[i]*10;
  //  a[i] = exp(2/5);
  int j = i*i;
    usleep(2000);
  }
}

void bar(int x)
{
  // do stuff...
  std::cout << "BARRRR Running...\n";

  for(int i=0;i<K/2;i++){
    //a[i] = a[i]*a[i];
    int j = i*i;
    usleep(2000);
  }
}

void show(){

    std::cout<<"The array is:"<<"\n";
    for(int i=0; i <K;i++){
    std::cout<<a[i]<<"\n";
    }
}

int main()
{
    clock_t t1,t2;
    t1 = clock();
    makeArray();
  //  show();

  std::thread first (foo);     // spawn new thread that calls foo()
  std::thread second (bar,0);  // spawn new thread that calls bar(0)
   //show();

  std::cout << "main, foo and bar now execute concurrently...\n";

  // synchronize threads:
  first.join();                // pauses until first finishes
  second.join();               // pauses until second finishes
//show();
//    for(int i=0;i<K;i++){
//    int j = i*i;
//    //a[i] = a[i]*a[i];
//    usleep(2000);
//  }


  std::cout << "foo and bar completed.\n";
//show();
 t2 = clock();
 std::cout<<"Runtime:"<< (float)t2-(float)t1<<"\n";
  return 0;
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
letsBeePolite
  • 2,183
  • 1
  • 22
  • 37
  • 2
    Doubling your performance(reducing your runtime to half) takes more effort than just adding two threads to a program. – Robert Harvey Mar 08 '15 at 03:22
  • Because your assumption is fallacious. If it was true, using an infinite number of threads would reduce it to zero. – user207421 Mar 08 '15 at 03:25
  • I needed to square each and every elements of a large array. Lets say there 10000 elements. Then, can I have one thread doing that on first 5000 elements and the other one on rest elements. And since I have a multicore CPU, so shouldnt do the work in half time? If not then what would the way to use parallelization in C++11 – letsBeePolite Mar 08 '15 at 03:29
  • 1
    With the usleep's uncommented, then I would expect the threaded version to take about half the time as the unthreaded version. I doubt the computations you were previously doing were intensive enough for the parallelism gain to vastly outweigh the extra cost of starting the threads. Also, I wouldn't include any I/O (e.g. - cout's) inside your timings as they may massively throw it off. – jschultz410 Mar 08 '15 at 03:32
  • @RobertHarvey: Can you refer to an simple example where the runtime is reduced by the number of threads in C++? – letsBeePolite Mar 08 '15 at 03:32
  • The answer depends on a lot of variables. 1. The number of cores on your machine. 2. Whether the cores are physical or hyper-threaded. 3. How your OS schedules your threads (they may run within the same core). 4. Other processes that are running at the same time. – Jamerson Mar 08 '15 at 03:32
  • @jschultz410: Can you refer to an simple example, exhibiting the time reduction because of threads? in C++ – letsBeePolite Mar 08 '15 at 03:38
  • @Frank : I have Corei7 with 4 physical cores and 8 logical threads. Using Ubuntu14.04 with a single browser and 1 tab opened. Thats it, no other applications (apart from Ubuntu processes running) running. – letsBeePolite Mar 08 '15 at 03:40
  • Don't use ``clock`` on Linux. Take a look at this [post](http://stackoverflow.com/a/647613). – Nikolay K Mar 08 '15 at 03:47

1 Answers1

4

The problem is in your use of clock(). This function actually returns the total amount of CPU run time consumed by your program across all cores / CPUs.

What you are actually interested in is the wall clock time that it took for your program to complete.

Replace clock() with time(), gettimeofday() or something similar to get what you want.

EDIT - Here's the C++ way to do timers the way you want: http://www.cplusplus.com/reference/chrono/high_resolution_clock/now/

jschultz410
  • 2,849
  • 14
  • 22