To the Top
File:  root - text - computing - delphi - 2011 - 06 - tricks.txt
Tags: 计算机, Computing Tips, | English | Home Page | Category: Computing | 716 Views, 23308 Search Bots | 142 Words

Subscribe to Feed Burner | Browse | Archive
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 | 716 Views, 23308 Search Bots | 142 Words Subscribe to Feed Burner

Related Articles

  1. Plus One
  2. [Daily Problem] Course Prerequisites
  3. Algorithm Interview: No Adjacent Repeating Characters
  4. Daily Interview Problem: 3 Sum
  5. Non-decreasing Array with Single Modification
  6. Daily Interview Problem: Decode String
  7. Spectrum Master
  8. Daily Interview Problem: Minimum Removals for Valid Parenthesis
  9. Daily Interview Question: Create a Simple Calculator
  10. Algorithm Interview: Shifted String

Comments (0)

    Be the first one to comment this page !


Page Edited: October 30 2020 14:21:09 | RSS Subscription
How to Cook a Perfect Steak? | <meta name="robots" content="index, follow">