//---------------------------------------------------------------------------

#include <vcl.h>
#include <time.h>
#include <windows.h> 
#include <process.h>
#include <stdio.h>
#pragma hdrstop

//---------------------------------------------------------------------------

#pragma argsused
int call = 0;
int loop = 10000000;
clock_t start;
void Func(int num)
{
	clock_t t = clock();
    printf("第%d次呼叫(開始)\t距離主程式開始經過：%lf\n",num,(double)(clock()-start)/CLOCKS_PER_SEC);
	double sum = 0;
	for(int i = 0 ; i < loop ; i++)
	{
		sum += (double)(rand()%10000)/10000.0;
	}
	printf("第%d次呼叫(結束)\t線程：%4d\t總和：%lf\t耗時：%lf\n",num, GetCurrentThreadId(),sum,(double)(clock()-t)/CLOCKS_PER_SEC);
}
unsigned int __stdcall ThreadFun(PVOID pM)
{
    call++;
    Func(call);
    return 0;
}
int main(int argc, char* argv[])
{
    start = clock();
	const int THREAD_NUM = 9;
    HANDLE handle[THREAD_NUM];
    for (int i = 0; i < THREAD_NUM; i++)
        handle[i] = (HANDLE)_beginthreadex(NULL, 0, ThreadFun, NULL, 0, NULL);
    WaitForMultipleObjects(THREAD_NUM, handle, TRUE, INFINITE);
	printf("使用線程的總時間：%lf\n",(double)(clock()-start)/CLOCKS_PER_SEC);
    start = clock();
    for (int i = 0; i < THREAD_NUM; i++)
        Func(i+1);
    printf("不使用線程的總時間：%lf\n",(double)(clock()-start)/CLOCKS_PER_SEC);
	system("PAUSE");
    return 0;
}
//---------------------------------------------------------------------------
/**********************************************
第1次呼叫(開始) 距離主程式開始經過：0.000000
第2次呼叫(開始) 距離主程式開始經過：0.000000
第3次呼叫(開始) 距離主程式開始經過：0.000000
第4次呼叫(開始) 距離主程式開始經過：0.000000
第5次呼叫(開始) 距離主程式開始經過：0.000000
第6次呼叫(開始) 距離主程式開始經過：0.016000
第7次呼叫(開始) 距離主程式開始經過：0.016000
第1次呼叫(結束) 線程：3480      總和：4694505.719100    耗時：0.438000
第8次呼叫(開始) 距離主程式開始經過：0.438000
第9次呼叫(開始) 距離主程式開始經過：0.438000
第4次呼叫(結束) 線程：3872      總和：4694505.719100    耗時：0.438000
第2次呼叫(結束) 線程：2844      總和：4694505.719100    耗時：0.438000
第3次呼叫(結束) 線程：2284      總和：4694505.719100    耗時：0.469000
第8次呼叫(結束) 線程：3724      總和：4694505.719100    耗時：0.453000
第9次呼叫(結束) 線程： 448      總和：4694505.719100    耗時：0.469000
第5次呼叫(結束) 線程：3472      總和：4694505.719100    耗時：0.907000
第7次呼叫(結束) 線程：3520      總和：4694505.719100    耗時：1.063000
第6次呼叫(結束) 線程：2828      總和：4694505.719100    耗時：1.063000
使用線程的總時間：1.079000
第1次呼叫(開始) 距離主程式開始經過：0.000000
第1次呼叫(結束) 線程：3892      總和：4694505.878300    耗時：0.406000
第2次呼叫(開始) 距離主程式開始經過：0.406000
第2次呼叫(結束) 線程：3892      總和：4692883.718700    耗時：0.422000
第3次呼叫(開始) 距離主程式開始經過：0.828000
第3次呼叫(結束) 線程：3892      總和：4692659.285499    耗時：0.406000
第4次呼叫(開始) 距離主程式開始經過：1.234000
第4次呼叫(結束) 線程：3892      總和：4694980.998601    耗時：0.422000
第5次呼叫(開始) 距離主程式開始經過：1.656000
第5次呼叫(結束) 線程：3892      總和：4692678.469100    耗時：0.406000
第6次呼叫(開始) 距離主程式開始經過：2.062000
第6次呼叫(結束) 線程：3892      總和：4693822.504300    耗時：0.422000
第7次呼叫(開始) 距離主程式開始經過：2.484000
第7次呼叫(結束) 線程：3892      總和：4694019.845800    耗時：0.406000
第8次呼叫(開始) 距離主程式開始經過：2.890000
第8次呼叫(結束) 線程：3892      總和：4693829.995600    耗時：0.422000
第9次呼叫(開始) 距離主程式開始經過：3.312000
第9次呼叫(結束) 線程：3892      總和：4694418.062700    耗時：0.406000
不使用線程的總時間：3.718000
請按任意鍵繼續 . . .
**********************************************/