提高计划绩效 [英] Improve program performance

查看:101
本文介绍了提高计划绩效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个javascript程序,用我的JSON数据执行操作(200兆)
这个程序通过regxp搜索我的数据中的出现

I have a javascript program which executes operations with my JSON data (200Mega) This programm search an occurences in my datas by regxp

var myDatas = [
 { "id" : "0000000001",
   "title" :"Data1",
   "info": {
             "info1": "data data data",
             "info2": "infoinfoiinfoinfo",
             "info3": "info333333333",
             "miscellaneous": "",
             "other": [
                 {"title": "qwe", "ref": "other"},
                 {"title": "sdsd", "ref": "other"},
                 {"title": "ddd", "ref": "123"} 
             ]
           },
    "text": "xxxx text sldjdjskldj text text" },
 . . . ];

实际执行速度太慢。

我会使用html 5的工作者,但IE9不支持IE9。

I would use Worker of html 5 but IE9 it's not supported by IE9.

我的节目:

    iterObject : function(obj){

        var text = $('#search-input').val() //text to search

        var self = this
        $.each(obj, function(key, value) {
            if((typeof value) == 'object'){
                self.iterObject(value)
            }
            else{
                 self.Search(text, value)
              }
          }
      }

Search : function(item, text){

        var regexThisFragment =
                          new RegExp("[^a-zA-Z0-9]+.{0,40}[^a-zA-Z]+" + 
                          item + 
                          "[^a-zA-Z]+.{0,40}[^a-zA-Z0-9]+", "i")

        while(text.match(regexThisFragment) != null)
        {
            var fragment = text.match(regexThisFragment).toString()
            console.log(fragment );
            }
        }
},

我如何改善我的程序?

推荐答案

发布的答案by user1689607 很有意思,但使用 setTimout
不能异步执行代码。它将在与主应用程序相同的
线程上运行。使用计时器
仍然有趣的是,在计时器运行时,应用程序的其他部分将有一些处理
时间。

The answer posted by user1689607 is interesting, but using setTimout will not execute the code asynchronously. It will be run on the same thread as the main application. What's still interesting in using a timer is that the other parts of the application will have some processing time available while timer is running.

让您的应用程序在慢速/快速设备上正常工作的最佳方法是选择处理时间和发布时间

由于处理数据可能需要一些时间,因此需要
才能取消它。
并使用回调来处理进程结束。

Best way to have your application working ok on slow/fast devices is to choose a processing time and a 'release' time. You also need, since processing data might take some time, to be able to cancel it. And use a callback to handle the end of process.

代码看起来像这样:

var DataProcessor = {
     dataProcessDuration : 30,
     releaseDuration : 5,
     dataProcessedCallback : function() { 
                //... somehow notify the application the result is available
                                       },
     _currentIndex : 0,
     _processing : false,
     _shouldCancel : false,
     processData : function() 
                              {   this.cancelProcessing();
                                  this._currentIndex =0;
                                  this._processing = true;
                                  // launch data process now, or give it time to cancel
                                  if (!this._shouldCancel)  this._processData();
                                  else setTimeout(this._processData.bind(this), 
                                            this.releaseDuration+this.dataProcessDuration+1);
                               },
     cancelProcessing : function() 
                               { 
                                     if (!this._processing) return;
                                     this._shouldCancel=true;
                               },
     _processData : function() {
          if (this._shouldCancel) { this._shouldCancel = false ;  
                                    this._processing = false; 
                                    return; }
           var startTime = Date.now();
           // while there's data and processing time  left.
           while  ( ( this._currentIndex < length of data)
                       && (Date.now()-startTime < this.dataProcessDuration) )
                {    var thisBatchCount=10;
                     while (thisBatchCount--)
                           {  
                              // ... process the this._currentIndex part of the data
                             this._currentIndex++;
                             if ( this._currentIndex == length of data) break;
                            }
                }
           if (this._currentIndex == (length of data) ) // the end ?
                {     this._processing = false; 
                      this.dataProcessedCallback()
                 }
           else // keep on processing after a release time
                setTimeout(this._processData.bind(this), this.releaseDuration);
                           }
}

Rq:在主流程循环中你可能想要通过更改thisBatchCount来微调粒度。
如果太小,你会过度检查Date.now()浪费时间,太大的内部处理将花费太多时间,你将没有你选择的过程/释放比率。

Rq : in the main process loop you might want to fine-tune the granularity by changing thisBatchCount. If too small you waste time by over-checking Date.now(), and too big the inner processing will take too much time and you won't have the process/release ratio you have choosen.

这篇关于提高计划绩效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆