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、效果测试和验证

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

相关推荐
PfCoder7 小时前
C#中定时器之System.Timers.Timer
c#·.net·visual studio·winform
雨季66611 小时前
Flutter 三端应用实战:OpenHarmony 简易“动态内边距调节器”交互模式深度解析
javascript·flutter·ui·交互·dart
人工智能AI技术13 小时前
【C#程序员入门AI】本地大模型落地:用Ollama+C#在本地运行Llama 3/Phi-3,无需云端
人工智能·c#
北极糊的狐15 小时前
光标放在引入的xxx组件行(import { xxx } from ‘element-ui‘;)出现标黄,显示报错:无法解析符号 ‘xxx‘ 解决办法
ui
子春一16 小时前
Flutter for OpenHarmony:构建一个 Flutter 习惯打卡应用,深入解析周视图交互、连续打卡逻辑与状态驱动 UI
flutter·ui·交互
我的xiaodoujiao17 小时前
使用 Python 语言 从 0 到 1 搭建完整 Web UI自动化测试学习系列 44--将自动化测试结果自动推送至钉钉工作群聊
前端·python·测试工具·ui·pytest
MyBFuture17 小时前
C#数组详解:一维二维与交错数组
开发语言·windows·c#·visual studio·vision pro
LcVong18 小时前
WPF MediaPlayer获取网络视频流当前帧并展示图片完整范例
网络·wpf
UI设计兰亭妙微18 小时前
UI 设计新范式:从国际案例看体验与商业的融合之道
人工智能·ui·b端设计