Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
stackshrink/baseline.cpp
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
73 lines (60 sloc)
1.8 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <intrin.h> | |
#include <vector> | |
#include <iostream> | |
#include <iomanip> | |
#include <chrono> | |
#include <tchar.h> | |
#include <windows.h> | |
#include <Psapi.h> | |
DWORD GetPageSize() { | |
SYSTEM_INFO stSysInfo; | |
GetSystemInfo( &stSysInfo ); | |
return stSysInfo.dwPageSize; | |
} | |
DWORD g_dwProcessorPageSize = GetPageSize(); | |
__declspec(noinline) PBYTE GetStackPointer() { | |
return (PBYTE)_AddressOfReturnAddress() + 8; | |
} | |
void StackConsume( PBYTE pPtr, DWORD dwSizeExtra ) { | |
const DWORD page_size = g_dwProcessorPageSize; | |
for ( ; dwSizeExtra >= page_size; dwSizeExtra -= page_size ) { | |
// Move our pointer to the next page on the stack. | |
pPtr -= page_size; | |
// read from this pointer. If the page isn't allocated yet - it will be. | |
volatile BYTE nVal = *pPtr; | |
} | |
} | |
void StackConsume( DWORD dwSizeExtra ) { | |
PBYTE pPtr = GetStackPointer(); | |
StackConsume( pPtr, dwSizeExtra ); | |
} | |
class timer_t { | |
private: | |
std::chrono::time_point<std::chrono::high_resolution_clock> start_time; | |
public: | |
timer_t() { | |
start_time = std::chrono::high_resolution_clock::now(); | |
} | |
double stop() { | |
auto stop_time = std::chrono::high_resolution_clock::now(); | |
return std::chrono::duration<double>( stop_time - start_time ).count(); | |
} | |
}; | |
int main() { | |
int count = 1'000'000; | |
using think_fn = void(*)(); | |
std::vector< think_fn > thinks; | |
thinks.reserve( count ); | |
for ( int i = 0; i < count; ++i ) { | |
thinks.push_back( []() { | |
StackConsume( 900 * 1024 ); | |
} ); | |
} | |
timer_t timer; | |
for ( auto & think : thinks ) { | |
think(); | |
} | |
double elapsed = timer.stop(); | |
std::cout << "Thought for " << elapsed << " seconds." << std::endl; | |
return 0; | |
} |