void Application::Run() { Stopwatch watch; // 理想状况 60 帧每秒 const DWORD desiredDelta = 1000 / 30; // 上一次每帧用时 DWORD lastDelta = desiredDelta; while (true) { try { watch.Restart(); if (DoFrame()) break; // 本帧用时 lastDelta = static_cast<DWORD>(watch.Stop() * 1000); // Sleep 剩余时间 if (lastDelta < desiredDelta) Sleep(desiredDelta - lastDelta); } catch (...) { bool handled = false; OnUncaughtException(handled); // 异常未处理 if (!handled) throw; } } }
int main ( ) { using namespace Furrovine; using namespace Furrovine::Audio; using namespace Furrovine::Pipeline; //using namespace Furrovine::Graphics; const static uint32 samplerate = 48000; const static uint32 bitspersample = 16; const static uint32 channelcount = 2; PCMAudioData data = WavAudioLoader( )( "16bit.wav" ); AudioDevice baseaudiodevice( bitspersample, channelcount, samplerate ); RealtimeAudioDevice audiodevice( baseaudiodevice, milliseconds( 10 ) ); audiodevice.AddDSP( PerChannelDelay( audiodevice.PCMDescription( ), { seconds( 0.513 ), seconds( 0.490 ) }, real( 0.25 ) ) ); AudioBuffer buffer( data.Buffer(), 0, 0, 0, 0, 1 ); audiodevice.SubmitBuffer( buffer ); Stopwatch stopwatch; stopwatch.Start( ); while ( true ) { double seconds = stopwatch.ElapsedSeconds( ); if ( seconds < 1 ) continue; //audiodevice.SubmitBuffer( buffer ); stopwatch.Restart( ); } }
void Test1() { DIVIDING_LINE_1('-'); constexpr int Count = 2001001; #if HAS_BOOST boost::recursive_mutex osMutex; ostream_t os(std::cout, osMutex); #else ostream_t &os = std::cout; #endif Stopwatch sw; sw.Start(); ProducerConsumerContext context(os, Count, Count / 10, ENABLE_LOGGING); Producer p1(context, 0, Count / 2, 200); Producer p2(context, Count / 2, Count - Count / 2, 200); Consumer c1(context, Count / 3, 200); Consumer c2(context, Count / 3, 200); Consumer c3(context, Count - Count / 3 - Count / 3, 200); std::vector<std::thread> threads; threads.push_back(p1.Create()); threads.push_back(p2.Create()); threads.push_back(c1.Create()); threads.push_back(c2.Create()); threads.push_back(c3.Create()); for (auto &t : threads) t.join(); sw.Stop(); std::cout << "Ellapsed time: " << sw.GetElapsedMilliseconds() << "ms" << std::endl; std::cout << "Checking if test is passed...\n"; sw.Restart(); bool ok = context.IsTestPassed(); sw.Stop(); std::cout << "It takes " << sw.GetElapsedMilliseconds() << "ms to figure out if test is passed." << std::endl; std::cout << "Is test passed? " << std::boolalpha << ok << std::endl; }
void LruCacheTest::PerfTestHelper( int operationCount, size_t capacity, bool enableTrim) { //SetVerifyOutput localVerifySettings(VerifyOutputSettings::LogOnlyFailures); size_t bucketCount = 10240; LruCache<wstring, TestCacheEntry> cache(enableTrim ? capacity : 0, bucketCount); Random rand(static_cast<int>(DateTime::Now().Ticks)); vector<TestCacheEntrySPtr> entries; for (auto ix=0; ix<operationCount; ++ix) { entries.push_back(make_shared<TestCacheEntry>(GetRandomKey(rand))); } Stopwatch stopwatch; // Write stopwatch.Restart(); for (auto it=entries.begin(); it!=entries.end(); ++it) { auto entry = *it; bool success = cache.TryPutOrGet(entry); VERIFY_IS_TRUE(success); } stopwatch.Stop(); Trace.WriteInfo( TraceComponent, "LruCache-put: operations={0} elapsed={1} throughput={2} ops/s", cache.Size, stopwatch.Elapsed, (double)cache.Size / stopwatch.ElapsedMilliseconds * 1000); // Read stopwatch.Restart(); for (auto it=entries.begin(); it!=entries.end(); ++it) { shared_ptr<TestCacheEntry> result; bool success = cache.TryGet((*it)->GetKey(), result); VERIFY_IS_TRUE(success); } stopwatch.Stop(); Trace.WriteInfo( TraceComponent, "LruCache-get: operations={0} elapsed={1} throughput={2} ops/s", cache.Size, stopwatch.Elapsed, (double)cache.Size / stopwatch.ElapsedMilliseconds * 1000); Trace.WriteInfo( TraceComponent, "CacheSize: {0}", cache.Size); }
int main(int argc, char* argv[]) { if (argc != 6) { cout << "usage: ./a.out "; cout << "<coupling.arg> "; cout << "<evo.arg> "; cout << "<lattice.arg> "; cout << "<plaquette.arg> "; cout << "<retherm.arg> "; cout << endl; exit(EXIT_FAILURE); } // Get/set parameters CouplingArg coupling_arg( argv[1] ); double beta = coupling_arg.beta;; EvoArg evo_arg( argv[2] ); int n_conf = evo_arg.n_conf; int n_chkpt = evo_arg.n_chkpt; string cfg_file_stem = evo_arg.file_stem; LatticeArg lattice_arg( argv[3] ); int sites[4]; int sitesX[4]; for (int mu=0; mu<4; ++mu) { sites[mu] = lattice_arg.sites[mu]; if (sites[mu]%2 ==0) { sitesX[mu] = sites[mu]/2; } else { cout << "Numer of lattice sites must be even!" << endl; exit(EXIT_FAILURE); } } enum lat_state state = lattice_arg.state;; PlaquetteArg plaquette_arg( argv[4] ); int n_plaq = plaquette_arg.n_plaq; string plaq_file_stem = plaquette_arg.file_stem; RethermArg retherm_arg( argv[5]); int retherm_sweeps = retherm_arg.sweeps; std::cout << "Initializing the lattice" << "..." << std::endl; Lattice latticeX(sitesX); Lattice lattice(sites); std::cout << "Initializing the monster..." << endl;; PlaquetteMonster monster(sites, n_plaq); vector<double> **plaq; vector<double> ***plaqs; plaq = new vector<double> * [n_plaq]; plaqs = new vector<double> ** [n_plaq]; for (int i=0; i<n_plaq; ++i) { plaq[i] = new vector<double> [n_plaq]; plaqs[i] = new vector<double> * [n_plaq]; for (int j=0; j<n_plaq; ++j) { plaqs[i][j]= new vector<double> [retherm_sweeps]; } } std::stringstream ss; ofstream file; cout << setprecision(15); Stopwatch timer; int conf = 0; do { timer.Restart(); if ( conf%n_chkpt==0 ) { std::cout << "\t"; ss.str(std::string()); ss << cfg_file_stem << "-" << conf << ".bin"; if (!latticeX.Read(ss.str()) ) { std::cout << "Failed to find lattice : " << std::endl; std::cout << ss.str() << std::endl; exit(EXIT_FAILURE); }; std::cout << "\t"; std::cout << "Computing observables..." << std::endl; monster.Initialize(latticeX); for (int m=0; m<n_plaq/2; ++m) for (int n=0; n<n_plaq/2; ++n) { plaq[m][n].push_back( 1.0-monster.MeanPlaquette(m+1,n+1) ); } if (retherm_sweeps>0) { std::cout << "\t"; std::cout << "Refining lattice..." << std::endl; if ( !InterpolatedTwoCellBoundaryRefine(latticeX, lattice) ) { std::cout << "Failed to refine lattice!" << std::endl; exit(EXIT_FAILURE); } for(int cool_sweeps=0; cool_sweeps<50; ++cool_sweeps) { lattice.CoolUpdate(Subsets::two_cell_bulk_checker_board); } std::cout << "\t"; std::cout << "Performing rethermalization sweeps and computing observables..." << std::endl; for (int h=0; h<retherm_sweeps; ++h) { monster.Initialize(lattice); for (int m=0; m<n_plaq; ++m) for (int n=0; n<n_plaq; ++n) { plaqs[m][n][h].push_back( 1.0-monster.MeanPlaquette(m+1,n+1) ); } lattice.HeatBathUpdate(beta); } } } std::cout << "Total time for config " << conf << " is : " << timer.ElapsedTime() << std::endl; ++conf; } while (conf < n_conf); //// Write observables to a file //// for (int m=0; m<n_plaq; ++m) for (int n=0; n<n_plaq; ++n) { ss.str(std::string()); ss << plaq_file_stem << "-" << m+1 << "_" << n+1 << ".dat"; std::cout << "Writing observables to file : "; std::cout << ss.str() << std::endl; file.open(ss.str().c_str()); file << setprecision(15); for(int j=0; j<plaq[m][n].size(); ++j) { file << plaq[m][n][j] << std::endl; } file.close(); } for (int m=0; m<n_plaq; ++m) for (int n=0; n<n_plaq; ++n) for (int h=0; h<retherm_sweeps; ++h) { ss.str(std::string()); ss << plaq_file_stem << "-" << m+1 << "_" << n+1 << "-" << h << ".dat"; std::cout << "Writing observables to file : "; std::cout << ss.str() << std::endl; file.open(ss.str().c_str()); file << setprecision(15); for(int j=0; j<plaqs[m][n][h].size(); ++j) { file << plaqs[m][n][h][j] << std::endl; } file.close(); } //// DONE! //// for (int i=0; i<n_plaq; ++i) { for (int j=0; j<n_plaq; ++j) { delete [] plaqs[i][j]; } delete [] plaq[i]; delete [] plaqs[i]; } delete [] plaq; delete [] plaqs; cout << "Congratulations on a job... done!" << endl; exit(EXIT_SUCCESS); }
void Engine::StartGame() { myGameIsRunning = true; // Close the game when we hit the close button myEventHost->RegisterEvent<CloseButtonPressedEvent>([=](CloseButtonPressedEvent &ev) { myGameIsRunning = false; }); // Show the window myRenderer->GetWindow()->SetVisible(true); Stopwatch stopwatch; std::vector<RenderCommand> *currentRenderCommands = new std::vector<RenderCommand>(); float time = 0.f; while (myGameIsRunning == true) { float deltaTime = stopwatch.GetElapsedSeconds(); time += deltaTime; stopwatch.Restart(); myRenderer->TriggerEvents(); std::swap(currentRenderCommands, myNewRenderCommands); float progressToNextFixedUpdate = (myTimeAccumulator / myTimeStep); myTimeAccumulator += deltaTime; Matrix33f worldToViewport; Transformation cameraTransformation = mySceneHost->GetCurrentScene()->GetCamera().GetTransformation(); auto gameLogicWork = myThreadPool->QueueWorkItem(std::function<void()>([=] { while (myTimeAccumulator >= myTimeStep) { myEventHost->TriggerEvent(UpdateEvent(myTimeStep)); myEventHost->TriggerEvent(EndUpdateEvent()); myTimeAccumulator -= myTimeStep; } myEventHost->TriggerEvent(DrawEvent()); })); Vector2f windowSize = Vector2f(myRenderer->GetWindow()->GetSize()); worldToViewport *= Matrix33f::CreateScale(2.f / windowSize.x, -2.f / windowSize.y, 1.f) * Matrix33f::CreateTranslation(-cameraTransformation.Position.x, -cameraTransformation.Position.y) * Matrix33f::CreateRotateAroundZ(cameraTransformation.Rotation); myRenderer->SetWorldToViewportMatrix(worldToViewport); myRenderer->Clear(); RenderTarget &renderTarget = *myRenderer->GetRenderTarget(); for (size_t i = 0; i < currentRenderCommands->size(); i++) { RenderCommand currentCommand = (*currentRenderCommands)[i]; Vector2f position = currentCommand.previousPosition + (currentCommand.currentPosition - currentCommand.previousPosition) * progressToNextFixedUpdate; renderTarget.Render(currentCommand.texture, position); } currentRenderCommands->clear(); myRenderer->PresentBackBuffer(); float beginSleep = stopwatch.GetElapsedSeconds(); std::this_thread::sleep_for(std::chrono::microseconds(1)); // Wait for the game logic update to finish gameLogicWork->Wait(); if (mySceneHost->GetCurrentScene() == nullptr) { myGameIsRunning = false; } } delete currentRenderCommands; myEventHost->TriggerEvent(ExitingEvent()); }