Sunday, December 19, 2010
Sunday, October 10, 2010
Use the magic number
Here is a very interesting article http://www.matrix67.com/data/InvSqrt.pdf
Posted by
stanleyxu (2nd)
/ALL RIGHTS RESERVED/ at
4:43 PM
0
comments
Tags: math
Thursday, April 23, 2009
Potential memory leaks by initializing a record (revised)
Currently I am involved in some D projects. Language D has nothing to do with Delphi. It has a lot of great features: Design by Contract, Templates, immutable, Array-Slicing, etc. My teammates give me a lot of advices on software design. One thing just inspired me. so I revised my old article "Potential memory leaks by initializing a record".
The main point is that FillChar() looks more like an abuse of initializing a record. I suggest declare a const record (eg. EmptyRecord) instead of using FillChar(). If you are an expert, you will NOT make such a mistake, but it is still worth to tell the rest Delphi developers.
Posted by
stanleyxu (2nd)
/ALL RIGHTS RESERVED/ at
8:25 PM
0
comments
Tags: delphi
Thursday, October 23, 2008
LIKE is not case insensitive?
I was stuck by understanding the following SQL query:
SELECT name
FROM my_table
WHERE name LIKE '%Jack%'
There is nothing special. It just tries to pick up all "names" of "my_table", those values contain the word "Jack" (case sensitive). However, "jack" is also in the result. I cannot believe my eyes.
I googled "LIKE case insensitive" and got the answer:
The problem is caused by the collation of the database. The default collation of my database is "latin1_swedish_ci". The trailing "_ci" stands for case insensitive. I changed it to "utf8_bin", the problem has been solved.
Here are some useful links:
http://forums.mysql.com/read.php?20,202839,202839
http://dev.mysql.com/doc/refman/5.0/en/pattern-matching.html
http://dev.mysql.com/doc/refman/5.0/en/charset-charsets.html
Posted by
stanleyxu (2nd)
/ALL RIGHTS RESERVED/ at
6:15 PM
1 comments
Tags: tips
Wednesday, September 10, 2008
require_once()
If you used require_once(), you may have known one big issue of it: "If you give a relative path to the file you want to require, PHP will use the working directory as root, not the directory where this file is located." Your code won't have any problem, except the file to be required, requires some other files as well and unfortunately they are not in a same directory.
There are several solutions on the Internet.
- Put all PHP files into one folder. This works for tiny projects, but a flat folder structure is really ugly.
- Use a PHP framework, like Zend, CodeIgniter, CakePHP. They provide a certain function like $this->load() to find the correct path of these files and load them.
- If your code is not based on a framework, you can simply use
"require_once(dirname(__FILE__).'/path_to_file')" instead of
"require_once('path_to_file')". However this does not work on my xampp environment. My code hangs up when executing this line. (The problem seems not to be on the slash or backslash. Really no idea)
My Solution
Write a function to calculate the relative path from file directory to working directory. If you previously use "require_once('path_to_file')", now you write"require_once(cp('path_to_file'))". It has been tested on Windows.
if (!function_exists('cp'))
{
function cp($f) // change_path
{
if (($wp = getcwd()) === ($fp = dirname(__FILE__))) { return $f; }
$wp = explode('\\', $wp);
$fp = array_merge(explode('\\', $fp), explode('/', $f));
for ($i = 0, $p = min(count($wp), count($fp)); $i < $p; $i++)
{
if ($wp[$i] === $fp[$i])
{
array_shift($wp);
array_shift($fp);
$i--; $p--;
continue;
}
break;
}
$p = ''; foreach ($wp as $k) { $p .= '../'; }
return $p.implode('/', $fp);
}
}
Posted by
stanleyxu (2nd)
/ALL RIGHTS RESERVED/ at
8:08 AM
0
comments
Tags: tips
Saturday, August 9, 2008
Quick note on UnicodeString
Jan Goyvaerts has introduced some key information about Unicode in Delphi 2009.
http://www.micro-isv.asia/2008/08/get-ready-for-delphi-2009-and-unicode/
Marco Cantu has made a performance comparison between AnsiString and UnicodeString.
http://blog.marcocantu.com/blog/tiburon_unicode_video_5.html
Conclusion
According to the information, so much I have collected in the past a few months, I summarized them as the following points:- Simple projects can be upgraded to Delphi 2009 without any modifications. Projects with heavy string operations or file operations could be compiled as well. But they should be tested completely before release.
- UnicodeString is based on the same concept as AnsiString. So it is very fast and efficient, although it is a little bit slower than AnsiString.
- In compare with WideString much less memory will be allocated for UnicodeString. If it is not necessary, you had better switch WideStrings to UnicodeStrings for a performance boost.
UPDATE 1: Here and here are detailed performance tests
Posted by
stanleyxu (2nd)
/ALL RIGHTS RESERVED/ at
10:36 AM
0
comments
Tags: delphi
Tuesday, June 3, 2008
Google features in a shell
Just found an interesting site http://goosh.org/. It is powered by Google APIs.
Posted by
stanleyxu (2nd)
/ALL RIGHTS RESERVED/ at
2:13 PM
0
comments
Tags: google