代碼的效率問題看一下代碼

來源:互聯網
上載者:User

程式碼片段1:

    private static long l4 = (long)1024 * 1024 * 1024 * 1024;
        private static long l3 = (long)1024 * 1024 * 1024;
        private static long l2 = (long)1024 * 1024;
   public static string FileTotalSize(long fileSize)
        {
             if (fileSize >l4)
            {
                return (fileSize / l4).ToString("f1") + "TB";
            }
            else if (fileSize > l3)
            {
                return (fileSize / l3).ToString("f1")+"GB";
            }
            else if (fileSize > l2)
            {
                return (fileSize / l2).ToString("f1")+"MB";
            }
            else
            {
                return fileSize.ToString() + "KB";
            }
       }       

 程式碼片段2:

   private static long l4 = (long)1024 * 1024 * 1024 * 1024;
        private static long l3 = (long)1024 * 1024 * 1024;
        private static long l2 = (long)1024 * 1024;

    public static string FileTotalSize(long fileSize)
        {
            if (fileSize < 1024)
            {
                return fileSize + "KB";
            }
            else if (fileSize < l3)
            {
                return (fileSize / l2).ToString("f1") + "MB";
            }
            else if (fileSize < l4)
            {
                return (fileSize / l3).ToString("f1") + "GB";
            }
            else
            {
                return (fileSize / l4).ToString("f1") + "TB";
            }
        }

以上兩段代碼沒有什麼區別就是判斷檔案的大小,但是效率就不一樣了。

1:上傳檔案大於1g的檔案的機率是多少。

2:上傳檔案小於1M和小於1G的機率是多少

第一個程式碼片段如果上傳的檔案小於1g檔案比較多的話,每次都要進行至少三次判斷

第二程式碼片段至少要少一次判斷,對於一個web程式來說,第二段代碼提高的可是n陪的效率

~~~~~~~~~~~~~所以作為一個合格的web程式員一定要多方面考慮,多方面考量。

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.