首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >动态或静态地描述数据以加速发送数据

动态或静态地描述数据以加速发送数据
EN

Stack Overflow用户
提问于 2014-02-07 15:48:53
回答 1查看 94关注 0票数 0

我的数据在6000到600万之间,这些数据必须发送到串口。我每3-5毫秒发送一次数据到串口。在发送数据之前,我必须对其进行一些计算(例如,使用sin,因为某些数据),并且在chart.every时间上描述数据--我在图表上发送了一个数据,然后计算下一个数据,但是这种方法有点慢(有时需要超过3-5 ms的时间)。

下列方法能加速我的程序吗?它们是否正确?

1-首先完全计算移动图(例如,100 send (.0001*Index+10),用于x=0到5000000)并将其存储在数组中,然后当我将数据发送到端口时,必须在数组中找到数据,然后对数据进行描述和发送。 如上所示,但将其存储在文件中并从文件中读取。

假设这是我的代码:

代码语言:javascript
复制
namespace WPF_Toolkit_SpeedTest
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public ObservableCollection<ChartItem> Items { set; get; }

        public MainWindow()
        {
            InitializeComponent();
        }
        #region winmm.dll functions
        int _counter = 0;
        public delegate void TimerEventHandler(UInt32 id, UInt32 msg, ref UInt32 userCtx, UInt32 rsv1, UInt32 rsv2);

        /// <summary>
        /// A multi media timer with millisecond precision
        /// </summary>
        /// <param name="msDelay">One event every msDelay milliseconds</param>
        /// <param name="msResolution">Timer precision indication (lower value is more precise but resource unfriendly)</param>
        /// <param name="handler">delegate to start</param>
        /// <param name="userCtx">callBack data </param>
        /// <param name="eventType">one event or multiple events</param>
        /// <remarks>Dont forget to call timeKillEvent!</remarks>
        /// <returns>0 on failure or any other value as a timer id to use for timeKillEvent</returns>
        [DllImport("winmm.dll", SetLastError = true, EntryPoint = "timeSetEvent")]
        static extern UInt32 timeSetEvent(UInt32 msDelay, UInt32 msResolution, TimerEventHandler handler, ref UInt32 userCtx, UInt32 eventType);

        /// <summary>
        /// The multi media timer stop function
        /// </summary>
        /// <param name="uTimerID">timer id from timeSetEvent</param>
        /// <remarks>This function stops the timer</remarks>
        [DllImport("winmm.dll", SetLastError = true)]
        static extern void timeKillEvent(UInt32 uTimerID);

        TimerEventHandler tim;//TimerEventHandler tim = new TimerEventHandler(this.Link);
        public void Link(UInt32 id, UInt32 msg, ref UInt32 userCtx, UInt32 rsv1, UInt32 rsv2)
        {
            _counter++;
            if ((_counter % 5) == 0) //if ((_counter % 10) == 0)
            {
                Dispatcher.Invoke(new Action(()=>{
                    Items.Add(new ChartItem(_counter, 100*Math.Sin((.0002*_counter))));
                    if (_counter>1000)
                    {
                        Items.RemoveAt(0);
                    }
                }));
            }

            if (_counter > 10000)
            {
                timeKillEvent(id);
                Dispatcher.Invoke(new Action(() => { button1.Content = stp.ElapsedMilliseconds; }));
            }
        }
        uint timerId;
        #endregion 

        private void button1_Click(object sender, RoutedEventArgs e)
        {

            Items = new ObservableCollection<ChartItem>();
            lineChart.ItemsSource = Items;

            Items.Add(new ChartItem(0, 0));
            tim = new TimerEventHandler(this.Link);
            uint rsv = 0;
            stp.Start();
            button1.Content = stp.ElapsedMilliseconds;
            timerId = timeSetEvent(1, 1, tim, ref rsv, 1);
        }
        System.Diagnostics.Stopwatch stp = new System.Diagnostics.Stopwatch();
    }
    public class ChartItem : INotifyPropertyChanged
    {
        public ChartItem(double t, double v)
        {
            time = t;
            myVar = v;
        }

        private double time;

        public double Time
        {
            get { return time; }
            set
            {
                time = value;
                OnPropertyChanged("Time");
            }
        }

        private double myVar;

        public double Value
        {
            get { return myVar; }
            set
            {
                myVar = value;
                OnPropertyChanged("Value");
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void OnPropertyChanged(string propertyName)
        {
            if (this.PropertyChanged != null)
            {
                this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
}

这段代码只显示了描绘部分,但同时我也发送了数据。

EN

回答 1

Stack Overflow用户

发布于 2014-02-07 15:52:05

似乎您正在将I/O绑定代码和CPU绑定代码不必要地结合在一起。我想说的是,更合适的方法是异步(并立即)发送数据,然后等待,使其变得极其无痛。当然,除非你想要将这两部分捆绑在一起是有原因的。

而且,看起来您的问题可能会稍微复杂一些,因为一个简单的罪恶或原因不可能花费您整个毫秒的时间。您是否尝试过对您的应用程序进行分析以找出真正的问题所在?

例如,如果您在每一步重新创建图形,并在某些UI中显示它,则会出现大幅度的减速。不要让UI减慢您的底层通信速度。相反,刷新UI应该只需要偶尔进行一次,而且理想情况下应该与下面的数据流分开。

我发现很难相信简单的数学操作就足以降低代码的速度。我的电脑每毫秒就能完成超过十万次的罪恶操作--如果你在绘制图形的时候做了这么多事情,你就会做一些可怕的错误:D

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21632069

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档