- 论坛徽章:
- 36
|
本帖最后由 cokeboL 于 2012-10-10 21:07 编辑
回复 24# starwing83
这么搞的,创建好的都pause()了,线程多余的循环圈数只能增加平均时间,所以也没减去,师傅给看下;
vfork()这种除了那个什么简版uc内核上面好像必须用之外,其他的用起来怕bug,所以从来不用。- #include <stdio.h>
- #include <unistd.h>
- #include <stdlib.h>
- #include <string.h>
- #include <pthread.h>
- #include <time.h>
- void* handle(void *s)
- {
- pause();
- }
- int main()
- {
- int i, thread_num = 0, total_time;
- pthread_t tid;
- struct timeval t_begin = {0}, t_end = {0};
-
- gettimeofday(&t_begin, NULL);
- for(i = 0; i < 1000; i++){
- if(0 == pthread_create(&tid, 0, handle, 0))
- {
- thread_num++;
- }
- }
- gettimeofday(&t_end, NULL);
- printf("thread_num: %d\n", thread_num);
- total_time = (t_end.tv_sec - t_begin.tv_sec)*1000000 + (t_end.tv_usec - t_begin.tv_usec);
- printf("average time: %f\n", ((double)total_time) / ((double)thread_num));
- return 0;
- }
复制代码- #include <stdio.h>
- #include <unistd.h>
- #include <stdlib.h>
- #include <string.h>
- int main()
- {
- int i, fork_num = 0, total_time;
- pid_t pid;
- struct timeval t_begin = {0}, t_end = {0};
-
- gettimeofday(&t_begin, NULL);
- for(i = 0; i < 1000; i++){
- if(0 == (pid = fork()))
- {
- pause();
- }
- else if(pid > 0)
- {
- fork_num++;
- }
- }
- gettimeofday(&t_end, NULL);
- printf("fork_num: %d\n", fork_num);
- total_time = (t_end.tv_sec - t_begin.tv_sec)*1000000 + (t_end.tv_usec - t_begin.tv_usec);
- printf("average time: %f\n", ((double)total_time) / ((double)fork_num));
- return 0;
- }
复制代码 |
|