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

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

相关推荐
初九之潜龙勿用16 分钟前
C# 操作Word模拟解析HTML标记之背景色
开发语言·c#·word·.net·office
雨季66623 分钟前
Flutter 三端应用实战:OpenHarmony 简易点击计数器与循环颜色反馈器开发指南
开发语言·flutter·ui·ecmascript·dart
时光追逐者1 小时前
使用 MWGA 帮助 7 万行 Winforms 程序快速迁移到 WEB 前端
前端·c#·.net
老骥伏枥~2 小时前
【C# 入门】程序结构与 Main 方法
开发语言·c#
全栈师2 小时前
java和C#的基本语法区别
java·开发语言·c#
钰fly3 小时前
联合编程(加载单个工具,ini读写,图片读写,setting存储)
c#
雨季6664 小时前
Flutter 三端应用实战:OpenHarmony 简易“动态主题切换卡片”交互模式
flutter·ui·交互·dart
FuckPatience4 小时前
C# 对象初始化器对属性赋值vs构造函数里对属性赋值
c#
浩浩测试一下6 小时前
洪水猛兽攻击 Ddos Dos cc Drdos floods区别
安全·web安全·网络安全·系统安全·wpf·可信计算技术·安全架构
m0_748233176 小时前
C语言vsC#:核心差异全解析
c语言·开发语言·c#