[心得] 整數轉16進位

看板Ajax作者 (http://pyms.fly.to)時間17年前 (2007/08/09 20:25), 編輯推噓0(000)
留言0則, 0人參與, 最新討論串1/3 (看更多)
note:已經有更快的方法 請看344 --- 不曉得這邊可不可以貼這種javascript的心得@@a 以前寫16進位轉換的時候 寫出一個雖然很笨但是很直覺好用的方法 用字串索引數字外加用charAt暴力分割字串 最近又拿出來用 突然在想有沒有更快的處理方法 於是做以下測試 測試時使用ie6, opera 8, mozilla firefox 1.0.2 結論:用字串+charAt暴力搜尋最好 方法1,2基本上時間相差沒有很多,ie1跑的比2快 mozilla反之,可是都是0.05秒以內的差距 再說1的程式碼比較短... --- 編號 時間 測試項目 1 1953 用字串+charAt暴力切割 2 1969 改用字串表 3 2453 用fromCharCode加上小判斷 4 2360 用暴力字碼表+fromCharCode 5 2016 把1的位元運算改用mod 結論是速度下降 6 2594 把2的字串表改用charAt轉字元表 還是速度下降 7 2469 把3改寫用if else 有比3快可是還是不夠 8 3844 把4的fromCharCode改在建立陣列做 9 4500 把6的charAt(0)改在建立陣列時候做 10 2000 把1的charAt改由subString做 發現會稍慢 --- <script> function dec2hex(num){ var str16 = "0123456789ABCDEF"; var str = new Array; for(var i = 0; num > 0; i++){ var tmpNum = num & 0x0F; str[i] = str16.charAt(tmpNum); num >>= 4; } return str.reverse().join(""); } function dec2hex2(num){ var str16 = new Array("0","1","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F"); var str = new Array; for(var i = 0; num > 0; i++){ var tmpNum = num & 0x0F; str[i] = str16[tmpNum]; num >>= 4; } return str.join(""); } function dec2hex3(num){ var str16 = new Array("0","1","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F"); var str = new Array; for(var i = 0; num > 0; i++){ var tmpNum = num & 0x0F; str[i] = String.fromCharCode((tmpNum>9)?(tmpNum+0x30):(tmpNum+0x37)); num >>= 4; } return str.reverse().join(""); } function dec2hex4(num){ var str16 = new Array(0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39,0x41,0x42,0x43,0x44,0x45,0x46); var str = new Array; for(var i = 0; num > 0; i++){ var tmpNum = num & 0x0F; str[i] = String.fromCharCode(str16[tmpNum]); num >>= 4; } return str.reverse().join(""); } function dec2hex5(num){ var str16 = "0123456789ABCDEF"; var str = new Array; for(var i = 0; num > 0; i++){ var tmpNum = num % 16; str[i] = str16.charAt(tmpNum); num >>= 4; } return str.reverse().join(""); } function dec2hex6(num){ var str16 = new Array("0","1","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F"); var str = new Array; for(var i = 0; num > 0; i++){ var tmpNum = num & 0x0F; str[i] = str16[tmpNum].charAt(0); num >>= 4; } return str.reverse().join(""); } function dec2hex7(num){ var str16 = new Array("0","1","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F"); var str = new Array; for(var i = 0; num > 0; i++){ var tmpNum = num & 0x0F; if(tmpNum > 9){ str[i] = String.fromCharCode(tmpNum + 0x30); }else{ str[i] = String.fromCharCode(tmpNum + 0x37); } num >>= 4; } return str.reverse().join(""); } function dec2hex8(num){ var str16 = new Array( String.fromCharCode(0x30), String.fromCharCode(0x31), String.fromCharCode(0x32), String.fromCharCode(0x33), String.fromCharCode(0x34), String.fromCharCode(0x35), String.fromCharCode(0x36), String.fromCharCode(0x37), String.fromCharCode(0x38), String.fromCharCode(0x39), String.fromCharCode(0x41), String.fromCharCode(0x42), String.fromCharCode(0x43), String.fromCharCode(0x44), String.fromCharCode(0x45), String.fromCharCode(0x46)); var str = new Array; for(var i = 0; num > 0; i++){ var tmpNum = num & 0x0F; str[i] = str16[tmpNum]; num >>= 4; } return str.reverse().join(""); } function dec2hex9(num){ var str16 = new Array( "0".charAt(0), "1".charAt(0), "1".charAt(0), "2".charAt(0), "3".charAt(0), "4".charAt(0), "5".charAt(0), "6".charAt(0), "7".charAt(0), "8".charAt(0), "9".charAt(0), "A".charAt(0), "B".charAt(0), "C".charAt(0), "D".charAt(0), "E".charAt(0), "F".charAt(0)); var str = new Array; for(var i = 0; num > 0; i++){ var tmpNum = num & 0x0F; str[i] = str16[tmpNum]; num >>= 4; } return str.reverse().join(""); } function dec2hex10(num){ var str16 = "0123456789ABCDEF"; var str = new Array; for(var i = 0; num > 0; i++){ var tmpNum = num & 0x0F; str[i] = str16.substring(tmpNum,tmpNum + 1); num >>= 4; } return str.reverse().join(""); } function $(str){ return document.getElementById(str); } var dateTmp; function t1(){ dateTmp = new Date().getTime(); for(var i = 0; i < 65536; i++){ dec2hex(i); } $("d_t1").innerHTML = (((new Date().getTime()) - dateTmp)); } function t2(){ dateTmp = new Date().getTime(); for(var i = 0; i < 65536; i++){ dec2hex2(i); } $("d_t2").innerHTML = (((new Date().getTime()) - dateTmp)); } function t3(){ dateTmp = new Date().getTime(); for(var i = 0; i < 65536; i++){ dec2hex3(i); } $("d_t3").innerHTML = (((new Date().getTime()) - dateTmp)); } function t4(){ dateTmp = new Date().getTime(); for(var i = 0; i < 65536; i++){ dec2hex4(i); } $("d_t4").innerHTML = (((new Date().getTime()) - dateTmp)); } function t5(){ dateTmp = new Date().getTime(); for(var i = 0; i < 65536; i++){ dec2hex5(i); } $("d_t5").innerHTML = (((new Date().getTime()) - dateTmp)); } function t6(){ dateTmp = new Date().getTime(); for(var i = 0; i < 65536; i++){ dec2hex6(i); } $("d_t6").innerHTML = (((new Date().getTime()) - dateTmp)); } function t7(){ dateTmp = new Date().getTime(); for(var i = 0; i < 65536; i++){ dec2hex7(i); } $("d_t7").innerHTML = (((new Date().getTime()) - dateTmp)); } function t8(){ dateTmp = new Date().getTime(); for(var i = 0; i < 65536; i++){ dec2hex8(i); } $("d_t8").innerHTML = (((new Date().getTime()) - dateTmp)); } function t9(){ dateTmp = new Date().getTime(); for(var i = 0; i < 65536; i++){ dec2hex9(i); } $("d_t9").innerHTML = (((new Date().getTime()) - dateTmp)); } function t10(){ dateTmp = new Date().getTime(); for(var i = 0; i < 65536; i++){ dec2hex10(i); } $("d_t10").innerHTML = (((new Date().getTime()) - dateTmp)); } </script> <input type=button onclick="t1();"/><span id="d_t1"></span><br/> <input type=button onclick="t2();"/><span id="d_t2"></span><br/> <input type=button onclick="t3();"/><span id="d_t3"></span><br/> <input type=button onclick="t4();"/><span id="d_t4"></span><br/> <input type=button onclick="t5();"/><span id="d_t5"></span><br/> <input type=button onclick="t6();"/><span id="d_t6"></span><br/> <input type=button onclick="t7();"/><span id="d_t7"></span><br/> <input type=button onclick="t8();"/><span id="d_t8"></span><br/> <input type=button onclick="t9();"/><span id="d_t9"></span><br/> <input type=button onclick="t10();"/><span id="d_t10"></span><br/> -- ▂▃◣ telnet://whshs.twbbs.org P_mingray ▂▃◣ telnet://bbs.kkcity.com.tw KKCity mingray ▃▊▌▆ telnet://wretch.twbbs.org P_mingray ▄▋▋▅ 個人網站 http://pyms.fly.to/ MingRay隨意閒聊 填色工具1.1.7感謝大家的支持與愛用 1.1.8版目前測試中 -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 59.115.131.243 ※ 編輯: nosql 來自: 59.115.131.243 (08/09 20:26) ※ 編輯: nosql 來自: 59.115.131.243 (08/10 00:05) ※ 編輯: nosql 來自: 59.115.131.243 (08/10 00:05)
文章代碼(AID): #16kmVGlZ (Ajax)
文章代碼(AID): #16kmVGlZ (Ajax)