조금 잉여로울진 몰라도... 삼항연산자와 곱셈의 성능 테스트

vani2의 이미지

#include <iostream>
#include <ctime>
 
using namespace std;
 
int main(int argc, char* argv[]) {
	unsigned int elapsedTime;
 
	elapsedTime = time(nullptr);
	for(int i=0 ; i < 1000 ; i++) {
		/*
		cout<<i + (rand()%20 < 10) * 4<<endl;
		cout<<i + (rand()%20 < 10) * 4<<endl;
		cout<<i + (rand()%20 < 10) * 4<<endl;
		cout<<i + (rand()%20 < 10) * 4<<endl;
		cout<<i + (rand()%20 < 10) * 4<<endl;
		cout<<i + (rand()%20 < 10) * 4<<endl;
		cout<<i + (rand()%20 < 10) * 4<<endl;
		cout<<i + (rand()%20 < 10) * 4<<endl;
		cout<<i + (rand()%20 < 10) * 4<<endl;
		cout<<i + (rand()%20 < 10) * 4<<endl;
		cout<<i + (rand()%20 < 10) * 4<<endl;
		cout<<i + (rand()%20 < 10) * 4<<endl;
		*/
		cout<<i + (rand()%20 < 10 ? 4 : 0)<<endl;
		cout<<i + (rand()%20 < 10 ? 4 : 0)<<endl;
		cout<<i + (rand()%20 < 10 ? 4 : 0)<<endl;
		cout<<i + (rand()%20 < 10 ? 4 : 0)<<endl;
		cout<<i + (rand()%20 < 10 ? 4 : 0)<<endl;
		cout<<i + (rand()%20 < 10 ? 4 : 0)<<endl;
		cout<<i + (rand()%20 < 10 ? 4 : 0)<<endl;
		cout<<i + (rand()%20 < 10 ? 4 : 0)<<endl;
		cout<<i + (rand()%20 < 10 ? 4 : 0)<<endl;
		cout<<i + (rand()%20 < 10 ? 4 : 0)<<endl;
		cout<<i + (rand()%20 < 10 ? 4 : 0)<<endl;
		cout<<i + (rand()%20 < 10 ? 4 : 0)<<endl;
		}
	cout<<time(nullptr) - elapsedTime<<" 초 걸림"<<endl;
 
 
	return 0;
	}

위와 같이 성능테스트를 하니

1차 테스트
3항: 132초
곱셈: 137초

2차 테스트:
3항: 133초
곱셈: 132초

사실상 별로 차이가 안나는데요...

공극적으로 보았을때 두 연산의 차이는 어떨까요?

ifree의 이미지

일단 위 예제는 연산 보다는 입출력 과정에서 대부분의 시간을 소모하게 되므로,
결과값이 큰 의미를 가질 것 같지는 않네요.

vani2의 이미지

이런. 가장 중요한 부분을 간과해 버렸네요.

지적 감사합니다. :D

shint의 이미지

printf는 0초
cout은 3초가 걸리네요.

출력 속도가 매우 다릅니다.
시간이 정확하게 측정되는지는 확인해야 할거 같네요.

//http://codepad.org/
 
 
//printf 사용 0초
#include <stdio.h>
#include <time.h>
 
 
int main(int argc, char* argv[]) {
	unsigned int elapsedTime;
	unsigned int t;
        int i;
 
	elapsedTime = time(NULL);
	for(i=0 ; i < 1000 ; i++) {
		printf("%d\n", i + (rand()%20 < 10 ? 4 : 0) );
		printf("%d\n", i + (rand()%20 < 10 ? 4 : 0) );
		printf("%d\n", i + (rand()%20 < 10 ? 4 : 0) );
		printf("%d\n", i + (rand()%20 < 10 ? 4 : 0) );
		printf("%d\n", i + (rand()%20 < 10 ? 4 : 0) );
		printf("%d\n", i + (rand()%20 < 10 ? 4 : 0) );
		printf("%d\n", i + (rand()%20 < 10 ? 4 : 0) );
		printf("%d\n", i + (rand()%20 < 10 ? 4 : 0) );
		printf("%d\n", i + (rand()%20 < 10 ? 4 : 0) );
		printf("%d\n", i + (rand()%20 < 10 ? 4 : 0) );
		printf("%d\n", i + (rand()%20 < 10 ? 4 : 0) );
		printf("%d\n", i + (rand()%20 < 10 ? 4 : 0) );
		}
        t = time(NULL) - elapsedTime;
	printf("%d 초 걸림\n", t );
 
 
	return 0;
	}
 
 
 
 
 
//cout 사용 3초
 
#include <iostream>
#include <ctime>
 
using namespace std;
 
int main(int argc, char* argv[]) {
	unsigned int elapsedTime;
 
	elapsedTime = time(NULL);
	for(int i=0 ; i < 1000 ; i++) {
		cout<<i + (rand()%20 < 10 ? 4 : 0)<<endl;
		cout<<i + (rand()%20 < 10 ? 4 : 0)<<endl;
		cout<<i + (rand()%20 < 10 ? 4 : 0)<<endl;
		cout<<i + (rand()%20 < 10 ? 4 : 0)<<endl;
		cout<<i + (rand()%20 < 10 ? 4 : 0)<<endl;
		cout<<i + (rand()%20 < 10 ? 4 : 0)<<endl;
		cout<<i + (rand()%20 < 10 ? 4 : 0)<<endl;
		cout<<i + (rand()%20 < 10 ? 4 : 0)<<endl;
		cout<<i + (rand()%20 < 10 ? 4 : 0)<<endl;
		cout<<i + (rand()%20 < 10 ? 4 : 0)<<endl;
		cout<<i + (rand()%20 < 10 ? 4 : 0)<<endl;
		cout<<i + (rand()%20 < 10 ? 4 : 0)<<endl;
		}
	cout<<time(NULL) - elapsedTime<<" 초 걸림"<<endl;
 
 
	return 0;
	}

댓글 첨부 파일: 
첨부파일 크기
Plain text icon printf.txt57.38 KB
Plain text icon cout.txt57.38 KB

----------------------------------------------------------------------------
젊음'은 모든것을 가능하게 만든다.

매일 1억명이 사용하는 프로그램을 함께 만들어보고 싶습니다.
정규 근로 시간을 지키는. 야근 없는 회사와 거래합니다.

각 분야별. 좋은 책'이나 사이트' 블로그' 링크 소개 받습니다. shintx@naver.com