2013年4月27日 星期六

Stopwatch類 時間測量

時間測量

開發環境

  • OS:Windows 7 64bit
  • IDE:Visual Studio 2010
  • Project Type:Window Form Developer
  • Framework:ASP.NET 4

狀況描述:
  • 想要計算程式運算效能。

解決方法:
  • 使用System.Diagnostics.Stopwatch
繼承架構:

System.Object 
  System.Diagnostics.Stopwatch



範例:


  private void button1_Click(object sender, EventArgs e)

        {

            //宣告並建立程式執行計時器

            Stopwatch sw = new Stopwatch();

            //宣告字串輸出結果存放變數
            string result = "";

            //迴圈執行次數,100萬
            int count = 1000000;

            //開始計時
            sw.Start();

            for (int i = 0 ;  i < count ;  i++)
            {
                result = "abcdefghijklmnopqrstuvwxyz" + i + "abcdefghijklmnopqrstuvwxyz";
            }

            //停止計時
            sw.Stop();

            Console.WriteLine("Elapsed Time: " + sw.ElapsedMilliseconds + "ms");

            //如果在Web From下要使用Debug.WriteLine( ) 才能輸出
            //Debug.WriteLine("Elapsed Time: " + sw.ElapsedMilliseconds + "ms");
        }

  • 注意若你使用了+=串接,那麼result將會非常長,因為接了一百萬次的string長度,此時千萬別使用Console.WriteLine( ),如此將會造成系統無法回應,因為要將那麼大量的字傳輸出到Console Port上是一個很大的運算量。
輸出結果:

Elapsed Time: 330ms

這個範例中,迴圈中的運算式執行了一百萬次花了330ms。

1ms = 0.001s。



參考資料:



Visual Studio 開發Web ASP.NET Console.WriteLine 無法輸出

Console.WriteLine 無法在 console Port 介面輸出

開發環境

  • OS:Windows 7 64bit
  • IDE:Visual Studio 2010
  • Project Type:WebForm Developer
  • Framework: ASP.NET


狀況描述:

  • 在Web Project 進入Debug模式時,使用Console.WriteLine無法顯示於輸出視窗,以至於無法做追蹤測試。

解決方法:

  • 在Web Project專案類型,應改用 Debug.WriteLine( )替代Console.WriteLine( )。
  • Debug.WriteLine( ) 需using System.Diagnostics
  • 另種方式可使用Response.Write( ),直接輸出於瀏覽器頁面上。

Visual Studio Debug 出現 未啟用偵錯對話方塊

未啟用偵錯

開發環境

  • OS:Windows 7 64bit
  • IDE:Visual Studio 2010
  • Project Type:WebForm Developer
  • Framework: ASP.NET


狀況描述:

  • Visual Studio 開發工具在 Web Developer 開發時,按下Debug按鈕,出現警告視窗提示「未啟用偵錯」。


過程圖片:















解決方法:

  • 開啟web.config檔,修改 <compilation/>裡的屬性debugtrue即可。
  • 這個屬性當要將網站發佈到web server時記得要將其設回false。





<configuration>

    <connectionStrings>
        <add name="ConnectionString" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True"
            providerName="System.Data.SqlClient" />
    </connectionStrings>
    <system.web>
        <compilation debug="true" targetFramework="4.0" />
    </system.web>
   
</configuration>
























參考資料:



官方技術參考文件

ActionScript3 Reference

Visual C#

ASP.NET 4

2012年10月9日 星期二

JavaScript For 與 For in

For 與 For In
  • for 功用是迭代陣列,用於陣列或類似陣列的物件。
  • for in 功用是列舉非陣列的物件,就是一一列舉出非陣列物件的屬性。
Note:

  技術上for in也可以用於Array,因為在JavaScript環境中,Array也是物件,但是不要這麼做,因為fon in不像for會確保迭代的順序,另外若是物件有客製化擴充功能,可能會導致邏輯上的錯誤發生。


在使用for in列舉物件時可以使用hasOwnProperty()來過濾掉來自原型鏈(prototype chain)上的屬性。



<script>
(function(window, undefined){    
    var car = {
        name:'Golf',
        color:"Black" 
    };
    
    //假設在程式的其他地方有人對Object原生物件增加了一個方法
    if(typeof Object.prototype.printName === 'undefined'){
        Object.prototype.printHaHa = function(){
            console.log('ha ha');
        }
    }
    
    
    //使用fon in 列舉屬性時    
    for(var attribute in car){
        console.log(attribute,':',car[attribute]);
    }   
    
    console.log('-------------------------------');
    
    //使用hasOwnProperty來過濾    
    for(var attribute in car){
        if(car.hasOwnProperty(attribute)){
            console.log(attribute,':',car[attribute]);
        }
    }
})(window);
</script>

chrome上的輸出:


printHaHa : function (){ console.log('ha ha'); } filter_pototype_attribute.html:25
------------------------------- filter_pototype_attribute.html:28
color : Black



我們可以看到,虛線分隔之後的輸出,不會列舉出printHaHa項目。

2012年9月20日 星期四

FSP 畫面更新率 與 動畫原理解說

FPS  (Frames Per Second )
畫面更新率,秒呈現的圖數,又稱為楨數。
  • 現在電影FPS為24,電視常使用30FPS。
  • 若是程式寫成的動畫需高於30FPS,若低於30FPS則會感覺不連貫感
  • 電影與程式動畫之顯像原理不同,因此FPS需求不同。


電影呈像:

Display frame1
Display frame2
Display frame3
Display frame4
And So On ..
程式動畫呈像:

Render frame1
Display frame1
Render frame2
Display frame2
And So On ..
互動式動畫成像:

  •  加入程式邏輯判斷,因此決定是否顯像,亦可加入邏輯與使用者互動。


Get
initial
state


Render
frame


Apply
rules


Done?



yes


Display frame

↑______________________________| No


程式動畫中FPS算法:

FPS若需要60

  •    1s /60 Frame   (每一frame 所需要的時間長度)

因此若是使用javascript程式來Render則:

function renderFrame(){
  //繪製的程式碼
}

window.setInterval(drawFrame, 1000/60);


如此在javascript環境中就可以每1000/60秒繪製一個圖像。

note:因為setInterval所使用的時間單位是ms因此使用1000,因為1000ms=1s。


  • int setInterval(function , time ms)。
  • setInterval這個funciton會依設定的時間值來呼叫所傳入的function。
  • setInterval被調用時會回傳一個timerId這個id是一個int,可用來給予 clearInterval API來做清除interval的動作。

參考資料:
參考書目: