File: root - text - computing - delphi - 2011 - 06 - tricks.txt
Tags: 计算机, Computing Tips, | English | Home Page | Category: Computing | 775 Views, 24967 Search Bots | 142 Words
| Browse | Archive
![Subscribe to Feed Burner Subscribe to Feed Burner](/images/form/rss.gif)
Tags: 计算机, Computing Tips, | English | Home Page | Category: Computing | 775 Views, 24967 Search Bots | 142 Words
![Subscribe to Feed Burner Subscribe to Feed Burner](/images/form/rss.gif)
Two tricks of making integer computation faster in Delphi.
1. Two integers a and b, to compute a mod b, where b is power of two (two, four, eight ... etc)...
in delphi, you write
result := a mod b;
in this case, to make it faster,
you can write
result := a and (b - 1); // b >= 2, and power of 2.
2. Two integers a (a >= 0) and b, to compute a div b, where b is power of two (two, four, eight ... etc)...
in delphi, you write
result := a div b;
in this case, to make it faster,
you can write
c := fun(b); // fun returns the integer(log2(b))
result := a shr c; // where b >= 2, and power of 2
// returns the left-most significant digit.
function fun(b: integer): integer; assembler; register;
asm
bsr eax, eax
end;
if (a < 0), it can be reworked as result = -((-a) shr c)
Tags: 计算机, Computing Tips, | English | Home Page | Cateogry: Computing | 775 Views, 24967 Search Bots | 142 Words 1. Two integers a and b, to compute a mod b, where b is power of two (two, four, eight ... etc)...
in delphi, you write
result := a mod b;
in this case, to make it faster,
you can write
result := a and (b - 1); // b >= 2, and power of 2.
2. Two integers a (a >= 0) and b, to compute a div b, where b is power of two (two, four, eight ... etc)...
in delphi, you write
result := a div b;
in this case, to make it faster,
you can write
c := fun(b); // fun returns the integer(log2(b))
result := a shr c; // where b >= 2, and power of 2
// returns the left-most significant digit.
function fun(b: integer): integer; assembler; register;
asm
bsr eax, eax
end;
if (a < 0), it can be reworked as result = -((-a) shr c)
![Subscribe to Feed Burner Subscribe to Feed Burner](/images/form/rss.gif)
Related Articles
- Daily Interview Problem: Given two arrays, write a function to compute their intersection.
- Daily Interview Problem: Min Range Needed to Sort
- How to Play WAV music under DOS?
- Daily Interview Question: Create a Simple Calculator
- Daily Interview Problem: Word Ordering in a Different Alphabetical Order
- Daily Interview Problem: Find the Number of Islands
- Daily Interview Problem: Distribute Bonuses
- Longest Substring Without Repeating Characters
- Daily Interview Problem: Maximum Profit From Stocks
- Windows Scripting
©2006~2024 SteakOverCooked - 0.0123 Seconds(s) - 1445.983 KB/s - 23 Online
Memory: 494.44 KB
18:54:01 up 13 days, 18:33, 2 users, load average: 0.98, 0.86, 0.73 - Server PHP Version: 7.4.33
How to Cook a Perfect Steak? | <meta name="robots" content="index, follow">
![](/images/top.jpg)
18:54:01 up 13 days, 18:33, 2 users, load average: 0.98, 0.86, 0.73 - Server PHP Version: 7.4.33
Read & Write - Normal - Mini - Post - All Comments - Statistics
Be the first one to comment this page !