이상해 : 함수 내에서 작업을 수행)기능 실행 시간이 나는이 두 가지 시나리오의 각 요소에 대한 조작과 연결리스트에 반복 사이의 시간 차이를 평가하기 위해 노력하고
1
2) 같은 장소에서 함수 호출없이 작업하기
나는 모든 호출에 대해 스택 프레임을 만들고 파괴하는 OS 오버 헤드로 인해 함수 호출의 변형이 훨씬 더 비용이 많이들 것이라고 예상했지만 결과는 다음과 같다. 그냥 정반대 였어. 나는 이유를 이해할 수 없었다. 누군가 일어난 일을 설명해 주시겠습니까?
이
내 프로그램입니다 :// ConsoleApplication4.cpp : Defines the entry point for the console application.
#include "stdafx.h"
#include<iostream>
#include<chrono>
#include<stdlib.h>
#define _CRT_SECURE_NO_WARNINGS
class linked_list_node
{
int a;
public :
std::string var;
bool eval()
{
if (var == "abc")
return true;
return false;
}
linked_list_node() { a = rand() % 100; if (a % 2 == 0) var = "abc"; }
linked_list_node* nxt;
std::string getVar() { return var; }
linked_list_node* getNext()
{
return nxt;
}
};
int _tmain(int argc, _TCHAR* argv[])
{
linked_list_node *head = new linked_list_node();
linked_list_node *trav = head;
int len = 75000;
while (len != 0)
{
linked_list_node *n = new linked_list_node();
trav->nxt = n;
trav = n;
len--;
}
trav->nxt = NULL;
//traversal with function
int length = 0;
trav = head;
std::chrono::steady_clock::time_point begin = std::chrono::steady_clock::now();
while (trav != NULL)
{
length++;
if (trav->eval())
std::cout << "";
trav = trav->nxt;
}
std::chrono::steady_clock::time_point end = std::chrono::steady_clock::now();
std::cout << "Time difference with function == " << std::chrono::duration_cast<std::chrono::nanoseconds>(end - begin).count() << std::endl;
//traversal without function
trav = head;
length = 0;
begin = std::chrono::steady_clock::now();
while (trav != NULL)
{
length++;
if (trav->var =="abc")
std::cout << "";
trav = trav->nxt;
}
end = std::chrono::steady_clock::now();
std::cout << "Time difference without function = " << std::chrono::duration_cast<std::chrono::nanoseconds>(end - begin).count() << std::endl;
getchar();
return 0;
}
이 내 결과입니다 :
기능 == 18100시차 기능이없는과
시간 차이 = 33700000
고마워, 실수 였어. –
도와 줘서 고마워! 늦게 답변 – cbuchart