Why does calling a function from a different location change the execution time?

I have to parse about 1.5 billion records that look roughly like this:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>* 1832 FETCH (UID 10585 RFC822.SIZE 46507 FLAGS () BODY[HEADER.FIELDS (From To Subject Date)] {182}
Subject: Some subject
From: SenderName <[email protected]>
To: [email protected]
Date: Wed, 14 Feb 2024 21:51:05 +0000
)
</code>
<code>* 1832 FETCH (UID 10585 RFC822.SIZE 46507 FLAGS () BODY[HEADER.FIELDS (From To Subject Date)] {182} Subject: Some subject From: SenderName <[email protected]> To: [email protected] Date: Wed, 14 Feb 2024 21:51:05 +0000 ) </code>
* 1832 FETCH (UID 10585 RFC822.SIZE 46507 FLAGS () BODY[HEADER.FIELDS (From To Subject Date)] {182}
Subject: Some subject
From: SenderName <[email protected]>
To: [email protected]
Date: Wed, 14 Feb 2024 21:51:05 +0000

)

Currently, I am trying to determine the fastest solution to this so I decided to set up a simple benchmark. Before I get yelled at for the terrible code, that is not the point of the question. I simply wrote the first thing that came to mind just so I could get the test set up.

I am on windows. The data file (emails.txt) is mapped to memory for faster read access. The file contains 112008 records for testing and is 29 MB in size.

I have the following code for parsing. Again the quality of the code is unimportant (unless that is what is causing this issue). This is because I will not be using string streams in the final code.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>std::tuple<const char*, DWORD> memory_map_file()
{
HANDLE file_handle = CreateFileW(L"emails.txt", GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_READONLY, NULL);
DWORD file_size = GetFileSize(file_handle, NULL);
HANDLE mapping_handle = CreateFileMappingW(file_handle, NULL, PAGE_READONLY, 0, 0, NULL);
LPVOID address = MapViewOfFile(mapping_handle, FILE_MAP_READ, 0, 0, 0);
return { (char*)address, file_size };
}
const NewMessageHeader parse_rfc822_header_string_stream(std::string_view header)
{
//Parse the message header using string streams...
}
void profiling_string_stream(const char* data, DWORD file_size)
{
std::string header;
header.reserve(3000);
for (DWORD i = 0; i < file_size - 1; ++i)
{
if (data[i] == '*' && data[i + 1] == ' ' && !header.empty())
{
const NewMessageHeader message_header = parse_rfc822_header_string_stream(header);
header.clear();
}
header += data[i];
}
}
const auto profile(void (*func)(const char*, DWORD), const char* data, DWORD file_size)
{
const auto start_scan{ std::chrono::steady_clock::now() };
profiling_scan(data, file_size);
const auto end_scan{ std::chrono::steady_clock::now() };
return std::chrono::duration<double>{ end_scan - start_scan };
}
int main()
{
//Memory map the file to remove OS operations overhead
const auto [data, file_size] = memory_map_file();
//ONE
const auto time_string_stream = profile(profiling_string_stream, data, file_size);
std::cout << "Finished string stream in " << time_string_stream.count() << std::endl;
//TWO
const auto start_string_stream{ std::chrono::steady_clock::now() };
profiling_string_stream(data, file_size);
const auto end_string_stream{ std::chrono::steady_clock::now() };
const std::chrono::duration<double> elapsed_seconds_string_stream{ end_string_stream - start_string_stream };
std::cout << "Finished string stream in " << elapsed_seconds_string_stream.count() << std::endl;
std::cin.get();
return 0;
}
</code>
<code>std::tuple<const char*, DWORD> memory_map_file() { HANDLE file_handle = CreateFileW(L"emails.txt", GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_READONLY, NULL); DWORD file_size = GetFileSize(file_handle, NULL); HANDLE mapping_handle = CreateFileMappingW(file_handle, NULL, PAGE_READONLY, 0, 0, NULL); LPVOID address = MapViewOfFile(mapping_handle, FILE_MAP_READ, 0, 0, 0); return { (char*)address, file_size }; } const NewMessageHeader parse_rfc822_header_string_stream(std::string_view header) { //Parse the message header using string streams... } void profiling_string_stream(const char* data, DWORD file_size) { std::string header; header.reserve(3000); for (DWORD i = 0; i < file_size - 1; ++i) { if (data[i] == '*' && data[i + 1] == ' ' && !header.empty()) { const NewMessageHeader message_header = parse_rfc822_header_string_stream(header); header.clear(); } header += data[i]; } } const auto profile(void (*func)(const char*, DWORD), const char* data, DWORD file_size) { const auto start_scan{ std::chrono::steady_clock::now() }; profiling_scan(data, file_size); const auto end_scan{ std::chrono::steady_clock::now() }; return std::chrono::duration<double>{ end_scan - start_scan }; } int main() { //Memory map the file to remove OS operations overhead const auto [data, file_size] = memory_map_file(); //ONE const auto time_string_stream = profile(profiling_string_stream, data, file_size); std::cout << "Finished string stream in " << time_string_stream.count() << std::endl; //TWO const auto start_string_stream{ std::chrono::steady_clock::now() }; profiling_string_stream(data, file_size); const auto end_string_stream{ std::chrono::steady_clock::now() }; const std::chrono::duration<double> elapsed_seconds_string_stream{ end_string_stream - start_string_stream }; std::cout << "Finished string stream in " << elapsed_seconds_string_stream.count() << std::endl; std::cin.get(); return 0; } </code>
std::tuple<const char*, DWORD> memory_map_file()
{
    HANDLE file_handle = CreateFileW(L"emails.txt", GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_READONLY, NULL);
    DWORD file_size = GetFileSize(file_handle, NULL);
    HANDLE mapping_handle = CreateFileMappingW(file_handle, NULL, PAGE_READONLY, 0, 0, NULL);
    LPVOID address = MapViewOfFile(mapping_handle, FILE_MAP_READ, 0, 0, 0);
    return { (char*)address, file_size };
}

const NewMessageHeader parse_rfc822_header_string_stream(std::string_view header)
{
    //Parse the message header using string streams...
}

void profiling_string_stream(const char* data, DWORD file_size)
{
    std::string header;
    header.reserve(3000);
    for (DWORD i = 0; i < file_size - 1; ++i)
    {
        if (data[i] == '*' && data[i + 1] == ' ' && !header.empty())
        {
            const NewMessageHeader message_header = parse_rfc822_header_string_stream(header);
            header.clear();
        }

        header += data[i];
    }
}

const auto profile(void (*func)(const char*, DWORD), const char* data, DWORD file_size)
{
    const auto start_scan{ std::chrono::steady_clock::now() };
    profiling_scan(data, file_size);
    const auto end_scan{ std::chrono::steady_clock::now() };
    return std::chrono::duration<double>{ end_scan - start_scan };
}

int main()
{
    //Memory map the file to remove OS operations overhead
    const auto [data, file_size] = memory_map_file();

    //ONE
    const auto time_string_stream = profile(profiling_string_stream, data, file_size);
    std::cout << "Finished string stream in " << time_string_stream.count() << std::endl;

    //TWO
    const auto start_string_stream{ std::chrono::steady_clock::now() };
    profiling_string_stream(data, file_size);
    const auto end_string_stream{ std::chrono::steady_clock::now() };
    const std::chrono::duration<double> elapsed_seconds_string_stream{ end_string_stream - start_string_stream };
    std::cout << "Finished string stream in " << elapsed_seconds_string_stream.count() << std::endl;

    std::cin.get();
    return 0;
}

Now for the interesting part, the portion labeled ONE runs in ~3.6 seconds where as the portion labeled TWO runs in ~5.5 seconds. This is a huge difference. Is there any reason this difference exists?

Here is the complete code: https://godbolt.org/z/dnfbY65G9

I should note this code is meaningless without the datafile and required windows.h to compile.
My question is why does this discrepancy of two seconds exist and how can I make sure my future benchmarks do not have this error.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật