Skip to content

Instantly share code, notes, and snippets.

@kennykerr
Last active September 28, 2021 23:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kennykerr/8ad47b799cf582a4014f52c4001ba49d to your computer and use it in GitHub Desktop.
Save kennykerr/8ad47b799cf582a4014f52c4001ba49d to your computer and use it in GitHub Desktop.
Language Overhead
// 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());
}
// 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);
}
}
// 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();
}
// 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();
}
// 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(())
}
// 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