简体   繁体   English

如何优化这种简单的数据(事件)转换类?

[英]How to optimize such simple data (event) casting Class?

So I have created compilable prototype for a graph element that can cast its data to subscribed functions. 因此,我为图形元素创建了可编译的原型,该元素可以将其数据转换为预订的函数。

//You can compile it with no errors.
#include <iostream>
#include <vector>

using namespace std ;

class GraphElementPrototype {

    // we should define prototype of functions that will be subscribers to our data
    typedef void FuncCharPtr ( char *) ;

public:
    //function for preparing class to work 
    void init()
    {
        sample = new char[5000];
    }
    // function for adding subscribers functions
    void add (FuncCharPtr* f)
    {
        FuncVec.push_back (f) ;
    } ;

    // function for data update
    void call()
    {
        // here would have been useful code for data update 
        //...
        castData(sample);
    } ;  

    //clean up init
    void clean()
    {
        delete[] sample;
        sample = 0;
    }

private:

    //private data object we use in "call" public class function
    char* sample;

    //Cast data to subscribers and clean up given pointer
    void castData(char * data){
        for (size_t i = 0 ; i < FuncVec.size() ; i++){
            char * dataCopy = new char[strlen(data)];
            memcpy (dataCopy,data,strlen(data));
            FuncVec[i] (dataCopy) ;}
    }

    // vector to hold subscribed functions
    vector<FuncCharPtr*> FuncVec ;

} ;


static void f0 (char * i) {  cout << "f0" << endl; delete[] i; i=0; }
static void f1 (char * i) {  cout << "f1" << endl; delete[] i; i=0; }

int main() {
    GraphElementPrototype a ;
    a.init();
    a.add (f0) ;
    a.add (f1) ;
    for (int i = 0; i<50000; i++)
    {
        a.call() ;
    }
    a.clean();
    cin.get();
}

Is it possible to optimize my data casting system? 是否可以优化我的数据投射系统? And if yes how to do it? 如果是的话,该怎么做?

  • Implement the program correctly and safely 正确,安全地实施程序
  • If performance Not Acceptable 如果表现不可接受
    • While Not Acceptable 虽然不可接受
      • Profile 轮廓
      • Optimize 优化
  • Done! 做完了!

In my experience, premature optimization is the devil. 以我的经验,过早的优化是魔鬼。

EDIT: 编辑:

Apparently while I was formatting my answer, another James ninja'd me with a similar answer. 显然,当我格式化答案时,另一个詹姆斯忍者给我提供了类似的答案。 Well played. 打的好。

Is it possible to optimize my data casting system? 是否可以优化我的数据投射系统? And if yes how to do it? 如果是的话,该怎么做?

If your program is not too slow then there is no need to perform optimizations. 如果您的程序不是太慢,则无需执行优化。 If it is too slow, then generally, improving its performance should be done like so: 如果速度太慢,则通常应按以下方式进行性能改进:

  1. Profile your program 分析您的程序
  2. Identify the parts of your program that are the most expensive 确定程序中最昂贵的部分
  3. Select from the parts found in step 2 those that are likely to be (relatively) easy to improve 从步骤2中找到的部分中选择可能(相对)容易改进的部分
  4. Improve those parts of the code via refactoring, rewriting, or other techniques of your choosing 通过重构,重写或您选择的其他技术来改进代码的这些部分

Repeat these steps until your program is no longer too slow. 重复这些步骤,直到您的程序不再太慢为止。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM