To the Top
File:  root - text - computing - delphi - 2011 - 06 - tricks.txt
Tags: 计算机, Computing Tips, | English | Home Page | Category: Computing | 721 Views, 23917 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 | 721 Views, 23917 Search Bots | 142 Words Subscribe to Feed Burner

Related Articles

  1. Daily Interview Problem: Min Range Needed to Sort
  2. Floor and Ceiling of a Binary Search Tree
  3. Sort a Partially Sorted List
  4. CVE-2015-8874 - cPanel EasyApache Vulnerabilities
  5. Daily Interview Problem: Group Words that are Anagrams
  6. Daily Interview Question: Find Cycles in a Graph
  7. Daily Interview Problem: Queue Using Two Stacks
  8. Daily Interview Problem: Given two arrays, write a function to compute their intersection.
  9. Algorithm Interview: Determine If Linked List is Palindrome
  10. Daily Interview Problem:Create a balanced binary search tree

Comments (0)

    Be the first one to comment this page !


Page Edited: May 11 2024 14:36:49 | RSS Subscription
How to Cook a Perfect Steak? | <meta name="robots" content="index, follow">