Measuring the Processing Time

Here, I will introduce the method of measuring the time to process the particular part of the program. In this example, the time to calculate all prime numbers of the composite number is measured. The essential form of this method is like below. The function clock() measures the CPU time, so you can get the time by dividing with CLOCKS_PER_SEC. You can find the bottle-neck part of your program with this method.

      clock_t start,end;
      start = clock();
      //Here, you can write code
      end = clock();
      printf("%.8fsec\n",(double)(end-start)/CLOCKS_PER_SEC);
      

Source Code of Prime Factor Decomposition

//
//  factor
//
//  Copyright 2015 Yuki Mochizuki
//

#include <iostream>
#include <math.h>

using namespace std;

long factor(long n1){
    long temp;
    
    while(n1%2==0){
        n1/=2;
        cout<<2<<endl;
    }
    
    temp=3;
    while(temp<=pow(n1,0.5)){
        if(n1%temp==0){
            n1/=temp;
            cout<<temp<<endl;
        }
        else{temp+=2;}
    }
    if(n1!=1){cout<<n1<<endl;}
    cout<<endl<<endl;
    return 0;
}

int main()
{
	
    long n1=1;
    clock_t start,end;
    while(n1 !=0){
        cout<<"input composite number<2147483647 (press 0 to close)"<<endl;
        cin>>n1;
        if(n1==0)return 0;
        
        cout<<"prime factor;"<<endl;
		start = clock();
        factor(n1);
        end = clock();
		printf("%.8fsec\n",(double)(end-start)/CLOCKS_PER_SEC);
    }
    return 0;
}