C# WPF上位机开发(函数运行时间分析)

【 声明:版权所有,欢迎转载,请勿用于商业用途。 联系信箱:feixiaoxing @163.com】

上位机除了基本功能和稳定性之外,还有一个要注意的就是运行效率的问题。如果我们想提高软件的运行效率,单位时间做更多的工作,或者是希望在最短的时间内完成某一个工作,这个时候就要优化一下函数的使用时间了。

测量的方法一般也比较简单,一般就是函数开头或者thread开头添加一个时间戳,在任务结束的地方也添加一个时间戳,两个时间戳的减法就是整个程序花费的时间。

1、设计界面

为了测试方便,我们画了一个界面,界面上面只有一个按钮,这样比较简单一点。

复制代码
<Window x:Class="WpfApp.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:sys="clr-namespace:System;assembly=mscorlib"
        xmlns:local="clr-namespace:WpfApp"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="600">
    <Grid>
        <StackPanel>
            <Button Content="Check Elapsed Time" Width="200" Height="80" Click="Button_Click" HorizontalAlignment="Center" VerticalAlignment="Top" Margin="100,50,100,0"/>
        </StackPanel>
    </Grid>
</Window>

对应的显示效果如下所示,

2、准备和实现代码

代码部分首先我们需要添加一个函数库,也就是Diagnostics库,这里面有我们需要的Stopwatch结构体。这个结构体大家可以看成是一个秒表。如果按下去,开始计时。再按下去的时候,停止计时。这个时候,我们再看一下秒表,就知道函数执行花费了多少时间了。

为了测试花费了多少时间,我们还添加了一个DoSomeOperation函数。在实际应用中,我们将它替换成自己需要测试的thread或者函数就可以了。

复制代码
using System;
using System.Collections.Generic;
using System.IO;
using System.Security.Cryptography;
using System.Text;
using System.Windows;

using System.Diagnostics;

namespace WpfApp
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {

        // construct function
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            // create Stopwatch instance
            Stopwatch stopwatch = new Stopwatch();

            // begin to start
            stopwatch.Start();

            // do some operation
            DoSomeOperation();

            // stop now
            stopwatch.Stop();

            // output final result
            MessageBox.Show($"Execute Time: {stopwatch.ElapsedMilliseconds} ms");
        }

        private void DoSomeOperation()
        {
            // simulate some operation
            for (int i = 0; i < 100000000; i++)
            {
                // some cpu operation
            }
        }
    }
}

3、效果测试和验证

测试的话,比较简单,直接编译之后,单击按钮即可。

相关推荐
LateFrames16 小时前
WinForms + OpenTK (OpenGL 3.3) 粒子动画实测:100 万粒子,流畅无压力
ui·性能优化
工程师00716 小时前
MQTT 概念详解与 C# 实战
开发语言·c#·mqtt通信
王码码203517 小时前
Flutter for OpenHarmony:stomp_dart_client 打造实时消息引擎(企业级 WebSocket 通信标准) 深度解析与鸿蒙适配指南
网络·websocket·网络协议·flutter·ui·华为·harmonyos
bugcome_com19 小时前
C# 字符串(String)详解与常用操作示例
c#
游乐码20 小时前
c#运算符重载
开发语言·c#
游乐码21 小时前
c#继承的原则
开发语言·c#
游乐码21 小时前
c#内部类和分部类
开发语言·c#
忙碌5441 天前
2026年Flutter 3.16全栈实战:从UI到后端的一体化开发革命
flutter·ui
我命由我123451 天前
Photoshop - Photoshop 工具栏(68)内容感知移动工具
学习·ui·职场和发展·求职招聘·职场发展·学习方法·photoshop
qq_454245031 天前
GraphMindStudio 数据操作层解析:基于 SQLite 的封装与自动化存储
sqlite·c#