0

is it possible to calculate the computing time of a process based on the number of operations that it performs and the speed of the CPU in GHz? For example, I have a for loop that performs a total number of 5*10^14 cycles. If it runs on a 2.4 GHz processor, will the computing time in seconds be: 5*10^14/2.4*10^9 = 208333 s? If the process runs on 4 cores in parallel, will the time be reduced by four? Thanks for your help.

1 Answers1

0

No, it is not possible to calculate the computing time based just on the number of operations. First of all, based on your question, it sounds like you are talking about the number of lines of code in some higher-level programming language since you mention a for loop. So depending on the optimization level of your compiler, you could see varying results in computation time depending on what kinds of optimizations are done.

But even if you are talking about assembly language operations, it is still not possible to calculate the computation time based on the number of instructions and CPU speed alone. Some instructions might take multiple CPU cycles. If you have a lot of memory access, you will likely have cache misses and have to load data from disk, which is unpredictable.

Also, if the time that you are concerned about is the actual amount of time that passes between the moment the program begins executing and the time it finishes, you have the additional confounding variable of other processes running on the computer and taking up CPU time. The operating system should be pretty good about context switching during disk reads and other slow operations so that the program isn't stopped in the middle of computation, but you can't count on never losing some computation time because of this.

As far as running on four cores in parallel, a program can't just do that by itself. You need to actually write the program as a parallel program. A for loop is a sequential operation on its own. In order to run four processes on four separate cores, you will need to use the fork system call and have some way of dividing up the work between the four processes. If you divide the work into four processes, the maximum speedup you can have is 4x, but in most cases it is impossible to achieve the theoretical maximum. How close you get depends on how well you are able to balance the work between the four processes and how much overhead is necessary to make sure the parallel processes successfully work together to generate a correct result.

Daniel
  • 6,595
  • 9
  • 38
  • 70
  • I was in fact talking about code written in C++ under Windows, but I were not referring to the number of lines, but to the time it would take the process to complete the `for` loop. I'll try using the `fork` system call to use the four cores, but, if I understand correctly, there is no way to determine the actual amount of time that will pass before the program finishes executing. Thanks. – user3764418 Dec 26 '14 at 16:30
  • @user3764418 it's not as simple as just calling fork. You need to make sure that your code is truly parallelizable and then you need to make sure the work is properly distributed between the four processes – Daniel Dec 26 '14 at 16:33
  • Also fork doesn't guarantee that the processes run on separate cores – Daniel Dec 26 '14 at 16:33
  • I was thinking that I should perhaps try to optimize the nested for loop. This is the code snippet: `for(int i = 0; i < k-1; i++) { for(int j = i+1; j < k; j++) { if( s[i] - s[j] == 0 ) { s[j] = -1; } } }` `k` is a very large number. Can you suggest a way to optimise this? I'll look into parallelizing the code as well. – user3764418 Dec 26 '14 at 17:20