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;
}
9
  • 2
    Doubling your performance(reducing your runtime to half) takes more effort than just adding two threads to a program. Commented Mar 8, 2015 at 3:22
  • Because your assumption is fallacious. If it was true, using an infinite number of threads would reduce it to zero. Commented Mar 8, 2015 at 3: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 Commented Mar 8, 2015 at 3: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. Commented Mar 8, 2015 at 3:32
  • @RobertHarvey: Can you refer to an simple example where the runtime is reduced by the number of threads in C++? Commented Mar 8, 2015 at 3:32

1 Answer 1

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/

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.