Difference between revisions of "User talk:Strife Onizuka/int2hex"

From Second Life Wiki
Jump to navigation Jump to search
 
(3 intermediate revisions by one other user not shown)
Line 4: Line 4:


: I should probably also note that I've actually been experimenting with the following:
: I should probably also note that I've actually been experimenting with the following:
: <lsl>string int2hex(integer int) {
<lsl>string int2hex(integer int) {
string hex = "";
string hex = "";
{
{
Line 10: Line 10:
while (shift >= 4) {
while (shift >= 4) {
nybble = (int >> (shift -= 4)) & 0xF;
nybble = (int >> (shift -= 4)) & 0xF;
if (nybble > 7) {
if (9 < nybble) {
if (nybble > 11) {
if (12 < nybble) {
if (nybble > 13) {
if (14 < nybble) char = "F"; // 15
if (nybble > 14) char = "f"; // 15
else if (13 < nybble) char = "E"; // 14
else char = "e"; // 14
else char = "D"; // 13
} else {
if (nybble > 12) char = "d"; // 13
else char = "c"; // 12
}
} else {
} else {
if (nybble > 9) {
if (11 < nybble) char = "C"; // 12
if (nybble > 10) char = "b"; // 11
else if (10 < nybble) char = "B"; // 11
else char = "a"; // 10
else char = "A"; // 10
} else {
if (nybble > 8) char = "9"; // 9
else char = "8"; // 8
}
}
}
} else {
} else {
if (nybble > 3) {
if ((0 == nybble) && ("" == hex)) char = "";
if (nybble > 5) {
else char = (string)nybble; // 0-9
if (nybble > 6) char = "7"; // 7
else char = "6"; // 6
} else {
if (nybble > 4) char = "5"; // 5
else char = "4"; // 4
}
} else {
if (nybble > 1) {
if (nybble > 2) char = "3"; // 3
else char = "2"; // 2
} else {
if (nybble > 0) char = "1"; // 1
else if (hex != "") char = "0"; // 0
else char = "";
}
}
}
}
if (char != "") hex += char;
if ("" != char) hex += char;
}
}
}
}
Line 56: Line 32:
}</lsl>
}</lsl>
Rather oddly it seems to take up even less memory under Mono, and performs nearly as well (it's only about 2% behind yours). The memory is likely accounted for due to Mono's ridiculous overhead on function calls, since this code has none, which'll also be why it can keep up in spite of having a loop doing the work. Anyway, kudos for your neat int2hex function, I think I've finally got to grips which what it's actually doing, very inventive stuff!<br/>-- '''[[User:Haravikk_Mistral|Haravikk]]''' <sup><small>([[User_talk:Haravikk_Mistral|talk]]|[[Special:Contributions/Haravikk_Mistral|contribs]])</small></sup> 14:42, 2 October 2010 (UTC)
Rather oddly it seems to take up even less memory under Mono, and performs nearly as well (it's only about 2% behind yours). The memory is likely accounted for due to Mono's ridiculous overhead on function calls, since this code has none, which'll also be why it can keep up in spite of having a loop doing the work. Anyway, kudos for your neat int2hex function, I think I've finally got to grips which what it's actually doing, very inventive stuff!<br/>-- '''[[User:Haravikk_Mistral|Haravikk]]''' <sup><small>([[User_talk:Haravikk_Mistral|talk]]|[[Special:Contributions/Haravikk_Mistral|contribs]])</small></sup> 14:42, 2 October 2010 (UTC)
:Saw you added a note to the main page and remembered I'd posted the above code, which I've tweaked to what I'm actually using; I have no idea why I originally had branches for 0-9 when I could just cast those! -- <br/>-- '''[[User:Haravikk_Mistral|Haravikk]]''' <sup><small>([[User_talk:Haravikk_Mistral|talk]]|[[Special:Contributions/Haravikk_Mistral|contribs]])</small></sup> 13:49, 20 August 2013 (PDT)
:That is pretty cool. I don't recall seeing it before. You know me, I can't leave anything alone. I've rebalanced things so it will be about the same (a bit faster for "A" and "B"). I added an extra loop to eat through the null states. I made zero a special case because it allowed me to avoid a race condition caused by said new loop. I'm not sure if I would have thought of adding a variable to avoid the cost of the repeated "+=", that is truly inspired. -- '''[[User:Strife_Onizuka|Strife]]''' <sup><small>([[User talk:Strife_Onizuka|talk]]|[[Special:Contributions/Strife_Onizuka|contribs]])</small></sup> 23:14, 20 August 2013 (PDT)
<lsl>string int2hex(integer int) {
string hex = "";
if (int)
{
integer shift = 32; integer nybble; string char = "";
do ; while (!((int >> (shift -= 4)) & 0xF));
do {
if (10 < (nybble = ((int >> shift) & 0xF))) {
if (12 < nybble) {
if (14 < nybble) char = "F"; // 15
else if (13 < nybble) char = "E"; // 14
else char = "D"; // 13
} else {
if (11 < nybble) char = "C"; // 12
else char = "B"; // 11
}
} else {
if (9 < nybble) char = "A"; // 10
else char = (string)nybble; // 0-9
}
hex += char;
} while ((shift -= 4) >= 0);
} else {
hex = "0";
        }
return (hex = "") + hex;
}</lsl>

Latest revision as of 22:14, 20 August 2013

Efficient Hex

I didn't see any links to this from Efficient Hex, do you think maybe it should be linked in? I've been trying different int2hex functions and have found that this one, while a tiny bit heavier on memory is the fastest due to having no looping and never more than 6 function calls.
-- Haravikk (talk|contribs) 13:09, 2 October 2010 (UTC)

I should probably also note that I've actually been experimenting with the following:

<lsl>string int2hex(integer int) { string hex = ""; { integer shift = 32; integer nybble; string char = ""; while (shift >= 4) { nybble = (int >> (shift -= 4)) & 0xF; if (9 < nybble) { if (12 < nybble) { if (14 < nybble) char = "F"; // 15 else if (13 < nybble) char = "E"; // 14 else char = "D"; // 13 } else { if (11 < nybble) char = "C"; // 12 else if (10 < nybble) char = "B"; // 11 else char = "A"; // 10 } } else { if ((0 == nybble) && ("" == hex)) char = ""; else char = (string)nybble; // 0-9 }

if ("" != char) hex += char; } } if (hex == "") hex = "0"; return (hex = "") + hex; }</lsl> Rather oddly it seems to take up even less memory under Mono, and performs nearly as well (it's only about 2% behind yours). The memory is likely accounted for due to Mono's ridiculous overhead on function calls, since this code has none, which'll also be why it can keep up in spite of having a loop doing the work. Anyway, kudos for your neat int2hex function, I think I've finally got to grips which what it's actually doing, very inventive stuff!
-- Haravikk (talk|contribs) 14:42, 2 October 2010 (UTC)

Saw you added a note to the main page and remembered I'd posted the above code, which I've tweaked to what I'm actually using; I have no idea why I originally had branches for 0-9 when I could just cast those! --
-- Haravikk (talk|contribs) 13:49, 20 August 2013 (PDT)
That is pretty cool. I don't recall seeing it before. You know me, I can't leave anything alone. I've rebalanced things so it will be about the same (a bit faster for "A" and "B"). I added an extra loop to eat through the null states. I made zero a special case because it allowed me to avoid a race condition caused by said new loop. I'm not sure if I would have thought of adding a variable to avoid the cost of the repeated "+=", that is truly inspired. -- Strife (talk|contribs) 23:14, 20 August 2013 (PDT)

<lsl>string int2hex(integer int) { string hex = ""; if (int) { integer shift = 32; integer nybble; string char = ""; do ; while (!((int >> (shift -= 4)) & 0xF)); do { if (10 < (nybble = ((int >> shift) & 0xF))) { if (12 < nybble) { if (14 < nybble) char = "F"; // 15 else if (13 < nybble) char = "E"; // 14 else char = "D"; // 13 } else { if (11 < nybble) char = "C"; // 12 else char = "B"; // 11 } } else { if (9 < nybble) char = "A"; // 10 else char = (string)nybble; // 0-9 } hex += char; } while ((shift -= 4) >= 0); } else { hex = "0";

       }

return (hex = "") + hex; }</lsl>