Last active
September 28, 2021 23:11
-
-
Save kennykerr/8ad47b799cf582a4014f52c4001ba49d to your computer and use it in GitHub Desktop.
Language Overhead
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
// C++/winRT via https://github.com/microsoft/cppwinrt | |
#include "winrt/Windows.System.Power.h" | |
int main() | |
{ | |
auto start = std::chrono::high_resolution_clock::now(); | |
for (int i = 0; i < 10'000'000; i++) | |
{ | |
[[maybe_unused]] auto _ = winrt::Windows::System::Power::PowerManager::RemainingChargePercent(); | |
} | |
printf("%lld ms", std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::high_resolution_clock::now() - start).count()); | |
} |
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
// C#/WinRT via https://github.com/microsoft/cswinrt | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
var timer = new System.Diagnostics.Stopwatch(); | |
timer.Start(); | |
for (int i = 0; i < 10_000_000; i++) | |
{ | |
var _ = Windows.System.Power.PowerManager.RemainingChargePercent; | |
} | |
timer.Stop(); | |
System.Console.WriteLine("{0} ms", timer.ElapsedMilliseconds); | |
} | |
} |
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
// C++/CX built into the Visual C++ compiler | |
void App::OnLaunched(Windows::ApplicationModel::Activation::LaunchActivatedEventArgs^ e) | |
{ | |
auto start = std::chrono::high_resolution_clock::now(); | |
for (int i = 0; i < 10'000'000; i++) | |
{ | |
auto _ = Windows::System::Power::PowerManager::RemainingChargePercent; | |
} | |
auto elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::high_resolution_clock::now() - start).count(); | |
auto window = Windows::UI::Xaml::Window::Current; | |
auto box = ref new Windows::UI::Xaml::Controls::TextBox(); | |
box->Text = (ref new Platform::Box<int64_t>(elapsed))->ToString(); | |
window->Content = box; | |
window->Activate(); | |
} |
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
// WinRT support built into the CLR | |
protected override void OnLaunched(Windows.ApplicationModel.Activation.LaunchActivatedEventArgs e) | |
{ | |
var timer = new System.Diagnostics.Stopwatch(); | |
timer.Start(); | |
for (int i = 0; i < 10_000_000; i++) | |
{ | |
var _ = Windows.System.Power.PowerManager.RemainingChargePercent; | |
} | |
timer.Stop(); | |
var window = Windows.UI.Xaml.Window.Current; | |
var box = new Windows.UI.Xaml.Controls.TextBox(); | |
box.Text = timer.ElapsedMilliseconds.ToString(); | |
window.Content = box; | |
window.Activate(); | |
} |
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
// Windows crate for Rust via https://github.com/microsoft/windows-rs | |
windows::include_bindings!(); | |
fn main() -> windows::Result<()> { | |
let start = std::time::Instant::now(); | |
for _ in 0..10_000_000 { | |
let _ = Windows::System::Power::PowerManager::RemainingChargePercent()?; | |
} | |
println!("{} ms", start.elapsed().as_millis()); | |
Ok(()) | |
} |
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
// WRL provided by the Windows SDK | |
#include <chrono> | |
#include <wrl.h> | |
#include <windows.system.power.h> | |
#pragma comment(lib, "onecoreuap") | |
#define CHECK(expression) { HRESULT result = expression; if (FAILED(result)) return result; } | |
int main() | |
{ | |
CHECK(CoInitializeEx(0, COINIT_MULTITHREADED)); | |
auto start = std::chrono::high_resolution_clock::now(); | |
for (int i = 0; i < 10'000'000; i++) | |
{ | |
Microsoft::WRL::Wrappers::HStringReference class_name(RuntimeClass_Windows_System_Power_PowerManager); | |
Microsoft::WRL::ComPtr<ABI::Windows::System::Power::IPowerManagerStatics> factory; | |
CHECK(RoGetActivationFactory(class_name.Get(), IID_PPV_ARGS(&factory))); | |
int _ = 0; | |
CHECK(factory->get_RemainingChargePercent(&_)); | |
} | |
printf("%lld ms", std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::high_resolution_clock::now() - start).count()); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment