Re: [心得] 整數轉16進位

看板Ajax作者 (http://pyms.fly.to)時間17年前 (2007/08/10 00:04), 編輯推噓1(101)
留言2則, 1人參與, 最新討論串3/3 (看更多)
※ 引述《othree (OOO)》之銘言: : 剛剛一時性起找到的轉法 : var Hex = parseInt(Num.toString(), 10).toString(16); : 你要不要也比較看看效能如何 後來檢視一下寫法 應該是用 num.toString(16); 才對 缺點是出來的16進位是小寫=x=" 也有確認過opera8, ff1.0.2可以用 --- 觀察時間(ms)來看 1,2方法都遠遜於11,12的方法 原來toString還可以這樣用 就算要轉換成大寫也比用1,2的方法快上一倍以上 感謝提供此方法 --- no ie o ff description 1 1797 1172 3375 .charAt暴力切割字串 2 1704 1109 3594 暴力索引查表 3 2360 1531 4516 fromCharCode + ? : 方式判斷 4 2250 1453 4516 fromCharCode暴力查表 5 1906 1219 3312 1把位元運算換成餘數運算 效果下降 6 2500 1735 4515 2加上無謂的charAt(0) 效能下降 7 2344 1516 4812 3改用if-else 效能沒差 8 3610 2359 4859 4改用fromCharCode表 效能下降 9 4297 3515 11203 6把charAt(0)拉到外面 效能下降 10 1891 1375 3344 1改用.subString 11 297 287 578 .toString(16) 12 422 487 1078 .toString(16).toUpperCase() --- <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 dec2hex11(num){ return num.toString(16); } function dec2hex12(num){ return num.toString(16).toUpperCase(); } 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)); } function t11(){ dateTmp = new Date().getTime(); for(var i = 0; i < 65536; i++){ dec2hex11(i); } $("d_t11").innerHTML = (((new Date().getTime()) - dateTmp)); } function t12(){ dateTmp = new Date().getTime(); for(var i = 0; i < 65536; i++){ dec2hex12(i); } $("d_t12").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/> <input type=button onclick="t11();"/><span id="d_t11"></span><br/> <input type=button onclick="t12();"/><span id="d_t12"></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/10 00:12)

08/10 02:11, , 1F
推~~
08/10 02:11, 1F

08/10 02:11, , 2F
08/10 02:11, 2F
文章代碼(AID): #16kpih-O (Ajax)
討論串 (同標題文章)
文章代碼(AID): #16kpih-O (Ajax)