Archive for category Computers
Not so random thoughts about random
by someone16 on Apr.29, 2010, under Computers
For one of my websites, I needed solution for randomly displaying different rows from MySQL database. As probably everyone knows, the easiest solution is to use following SQL:
select * from table order by rand()
There are no problems with using that, except one: It's REALLY SLOW. This might not be a problem with table with only few rows, but with bigger tables it really decreases the performance.
After spending a few hours of Googling and testing different methods, I find one, that works really great.
Following query is a lot faster than order by rand() and works without a problem:
SELECT * FROM Table T JOIN (SELECT CEIL(MAX(ID)*RAND()) AS ID FROM Table) AS x ON T.ID >= x.ID LIMIT 1;
I found this solution on Jay Paroline's blog: http://wanderr.com/jay/order-by-slow/2008/01/30/
I wondered if there is an even faster approach to get random row and then it hit me. So simple solution and it works even faster.
Why use only MySQL if we're building a website? Why wouldn't we rather use MySQL in connection with PHP (or other language) to get random result?
Solution
$x = mysql_fetch_array(mysql_query("SELECT COUNT(*) FROM table"));
$y = $x[0]-1;
$x = rand(0,$y);
$query = mysql_query("SELECT * FROM table LIMIT ".$x.", 1");
Explanation
Firstly, we get count of all rows in table (this value can be memcached if we want even better performance).
Then we subtract 1 from total count and a get random number between 0 and (n-1).
After that we simply use limit to select 1 row at position $x.
I implemented this on my site and everything was a bit faster, but this solution can only fetch one row at a time. In my case I have to display 10 - 40 random rows at once.
So I searched for SQL to randomly get multiple rows and I found following solution:
mysql_query("SELECT * FROM (
SELECT @cnt := COUNT( * ) +1, @lim :=10
FROM table
)vars
STRAIGHT_JOIN (
SELECT table . * , @lim := @lim -1
FROM table
WHERE (
@cnt := @cnt -1
)
AND RAND( ".time(0)." ) < @lim / @cnt
)i");
Again we need to use something else than MySQL to get random seed in this case current time in seconds is used.
This is a great solution, but still slow.
I thought of using PHP's rand to randomly select rows but in that case I will have to have sequential IDs and because of that we won't be able to delete rows somewhere in the middle.
So I ended up with following PHP code:
Multiple rows solution
$query = mysql_query("select id from table");
$ids = array();
while($id = mysql_fetch_query($query)) {
$ids[] = $id[0];
}
$temp = '';
for($i = 0; $i < 40; $i++) {
$temp .= $ids[rand(0, count($ids)-1)].',';
}
$temp = substr($t, 0, strlen($t)-1);
$query = mysql_query("select * from table where id in (".$t.")");
Explanation
This code might seem a bit harder but it's really easy.
First of all we get all IDs from table and put it in array $ids.
Then we use for loop to select 40 random IDs from array and add it to $temp variable and add comma, to separate the.
After that we remove last comma and then select rows from table where id matches ones from $temp.
Result
In my case I have about 50 000 rows and I needed to display 40 random rows. With previous solution with joins the page executed in about 0.7 to 0.8 seconds.
With my solution I speed it up to about 0.1 - 0.15 seconds of execution time. This is about 85% performance improvement.
Leave a Comment
:mysql php performance rand random
more...
select * from table order by rand()
There are no problems with using that, except one: It's REALLY SLOW. This might not be a problem with table with only few rows, but with bigger tables it really decreases the performance.
After spending a few hours of Googling and testing different methods, I find one, that works really great.
Following query is a lot faster than order by rand() and works without a problem:
SELECT * FROM Table T JOIN (SELECT CEIL(MAX(ID)*RAND()) AS ID FROM Table) AS x ON T.ID >= x.ID LIMIT 1;
I found this solution on Jay Paroline's blog: http://wanderr.com/jay/order-by-slow/2008/01/30/
I wondered if there is an even faster approach to get random row and then it hit me. So simple solution and it works even faster.
Why use only MySQL if we're building a website? Why wouldn't we rather use MySQL in connection with PHP (or other language) to get random result?
Solution
$x = mysql_fetch_array(mysql_query("SELECT COUNT(*) FROM table"));
$y = $x[0]-1;
$x = rand(0,$y);
$query = mysql_query("SELECT * FROM table LIMIT ".$x.", 1");
Explanation
Firstly, we get count of all rows in table (this value can be memcached if we want even better performance).
Then we subtract 1 from total count and a get random number between 0 and (n-1).
After that we simply use limit to select 1 row at position $x.
I implemented this on my site and everything was a bit faster, but this solution can only fetch one row at a time. In my case I have to display 10 - 40 random rows at once.
So I searched for SQL to randomly get multiple rows and I found following solution:
mysql_query("SELECT * FROM (
SELECT @cnt := COUNT( * ) +1, @lim :=10
FROM table
)vars
STRAIGHT_JOIN (
SELECT table . * , @lim := @lim -1
FROM table
WHERE (
@cnt := @cnt -1
)
AND RAND( ".time(0)." ) < @lim / @cnt
)i");
Again we need to use something else than MySQL to get random seed in this case current time in seconds is used.
This is a great solution, but still slow.
I thought of using PHP's rand to randomly select rows but in that case I will have to have sequential IDs and because of that we won't be able to delete rows somewhere in the middle.
So I ended up with following PHP code:
Multiple rows solution
$query = mysql_query("select id from table");
$ids = array();
while($id = mysql_fetch_query($query)) {
$ids[] = $id[0];
}
$temp = '';
for($i = 0; $i < 40; $i++) {
$temp .= $ids[rand(0, count($ids)-1)].',';
}
$temp = substr($t, 0, strlen($t)-1);
$query = mysql_query("select * from table where id in (".$t.")");
Explanation
This code might seem a bit harder but it's really easy.
First of all we get all IDs from table and put it in array $ids.
Then we use for loop to select 40 random IDs from array and add it to $temp variable and add comma, to separate the.
After that we remove last comma and then select rows from table where id matches ones from $temp.
Result
In my case I have about 50 000 rows and I needed to display 40 random rows. With previous solution with joins the page executed in about 0.7 to 0.8 seconds.
With my solution I speed it up to about 0.1 - 0.15 seconds of execution time. This is about 85% performance improvement.
Switching to Bing
by someone16 on Mar.28, 2010, under Computers
I’ve been searching with Google from the first day of using the Internet. It’s great search engine, I find everything I want and it always works. Sometimes there are some problems with loading front page of Google, but apart from that I haven’t encounter any problems with it so far.
However, recently I’m testing Bing and I decided to completely switch to it from Google. I’m also unhappy with Google’s policy on keeping my personal information. I’ll only be using Bing search engine and stay with Gmail because it has better UI and I don’t want to change my email address. Of course Google Analytics, Webmaster Tools and other tools have no Microsoft alternative and I’m happy with them, so there is no reason for me to switch.
It’s never bad to check alternatives, and see if they are better (I doubt, but Bing Maps really suprised me in a good way).
More about Google:
http://www.youtube.com/watch?v=R7yfV6RzE30
Leave a Comment
:google bing
more...

However, recently I’m testing Bing and I decided to completely switch to it from Google. I’m also unhappy with Google’s policy on keeping my personal information. I’ll only be using Bing search engine and stay with Gmail because it has better UI and I don’t want to change my email address. Of course Google Analytics, Webmaster Tools and other tools have no Microsoft alternative and I’m happy with them, so there is no reason for me to switch.
It’s never bad to check alternatives, and see if they are better (I doubt, but Bing Maps really suprised me in a good way).
More about Google:
http://www.youtube.com/watch?v=R7yfV6RzE30
Security Rulz
by someone16 on Feb.08, 2010, under Computers
I would like to inform you that our website has a 128 bit encryption. With this base, passwords that comprise only of letters and alphabets create an algorithm that is difficult to crack. We discourage the use of special characters because hacking softwares can recignize them very easily.
The length of the password is limited to 8 characters to reduce keyboard contact. Some softwares can decipher a password based on information of “most common keys pressed”.
Therefore, lesser keys punched in a given frame of time lessen the possibility of the password being cracked.
iPad
by someone16 on Feb.07, 2010, under Computers

iPad? What’s that? A bigger iPhone!
So Apple announced their first Tablet PC, which is neither a tablet nor a PC. Speculations about what it will look like were all wrong. Apple didn’t make a usable tablet computer, but a friggin’ MP3 player with iBooks application and broken web browser.
Maybe my expectations were too high, but their product is really not something that could be called tablet. I expected something like normal PC tablets with very good battery and Mac OS X operating system with the possibility of using Windows via Boot Camp. However, the only thing they could have came up with was a bigger iPhone intended for reading eBooks and surfing the Internet with Safari (without Flash support!).
As usual, Apple will sell iPads without a problem and without a competition in this market.
ASP.Net MVC
by someone16 on Nov.03, 2009, under Computers
MS released MVC 1.0 for ASP.Net 3.5 a few months ago. I just didn't take time to learn about it.
And last week I learnt that it's very good addition and something that I had been waiting from the release of first ASP.Net framework. I rather programmed in ASP than in ASP.Net because of freedom. In ASP you are free to make your HTML code as you like. In ASP.Net you have some Web Controls which usually generated really nasty code. And you have no control over this code. Postbacks are pain in the ass too. Google and other search engines can't crawl web pages with postbacks because you need javascript to use them (Google recently announced that they were thinking about making javascript-compatible crawler, but for now no one has that capability).
In ASP.Net MVC you can use your own HTML code and do everything as you like. It's similar to ASP without a framework, except that you actually have a framework. Now you have a choice to use either Web Controls or MVC in your projects. And I also saw some web sites made with it already.
BTW classic ASP is still pretty much alive. There are a lot of sites made with them, I guess they just don't have time/experience/need to rewrite everything.
Leave a Comment
:ASP ASP.Net MVC
more...
And last week I learnt that it's very good addition and something that I had been waiting from the release of first ASP.Net framework. I rather programmed in ASP than in ASP.Net because of freedom. In ASP you are free to make your HTML code as you like. In ASP.Net you have some Web Controls which usually generated really nasty code. And you have no control over this code. Postbacks are pain in the ass too. Google and other search engines can't crawl web pages with postbacks because you need javascript to use them (Google recently announced that they were thinking about making javascript-compatible crawler, but for now no one has that capability).
In ASP.Net MVC you can use your own HTML code and do everything as you like. It's similar to ASP without a framework, except that you actually have a framework. Now you have a choice to use either Web Controls or MVC in your projects. And I also saw some web sites made with it already.
BTW classic ASP is still pretty much alive. There are a lot of sites made with them, I guess they just don't have time/experience/need to rewrite everything.
Nokia Booklet 3G
by someone16 on Aug.24, 2009, under Computers
Nokia today announced their first laptop, Windows based Nokia Booklet 3G. The Nokia Booklet weighs 1.2 kilograms and is 2cm thick (as opposite to Apple MacBook Air which weights a little bit more but is thinner with it's 1.94 cm in height). It supposed to have 12 hour battery life, 10" glass HD-ready display and integrated GPS.
Leave a Comment
:
more...
Key hardware information
- Intel Atom Chipset (Z530 running at 1.6 Ghz). No fan, which means near silent running.
- 120 GB hard disk
- 3G / HSDPA and WiFi connectivity, plus integrated Bluetooth for local wireless communication
- 12 hour battery life
- 19.9 mm thick x 264 mm width x 185 mm depth
- 10.1 inch glass HD ready display
- Front facing video camera mounted, on top of the screen, for video calling
- 1 x HDMI port (for HD video out), 1 x integarted SD card reader, 3 x USB ports, 1 x audio port
- Integrated A-GPS
- Stereo speakers mounted on the front right and left corners
Your location online
by someone16 on Aug.22, 2009, under Computers
Hi,
If you have an iPhone and a MobileMe subscription it's very easy to publish your location online.
Firstly you need to turn on "Find My iPhone" function on iPhone in Settings application and you need to make sure you have data pushed to the iPhone.
Then you need a PHP class for fetching your iPhone location through me.com (http://clickontyler.com/blog/2009/06/sosumi-a-mobileme-scraper/).
Code for fetching data from the server:
// Get the iPhone location from MobileMe
echo "Fetching iPhone location...";
$mobileMe = new Sosumi ($mobileMeUsername, $mobileMePassword);
$iphoneLocation = $mobileMe->locate();
echo "got it.";
echo "iPhone location: $iphoneLocation->latitude, $iphoneLocation->longitude";
echo "iPhone accuracy: $iphoneLocation->accuracy";
echo "<br />";
if($iphoneLocation->latitude > 0) {
echo file_get_contents("http://example.com/update-location/?latitude=".$iphoneLocation->latitude."&longitude=".$iphoneLocation->longitude."&accuracy=".$iphoneLocation->accuracy."&key=auth-key");
}
// Now update Google Latitude
//echo "Updating Google Latitude...";
//$google->updateLatitude($iphoneLocation->latitude, $iphoneLocation->longitude,
//<span style="white-space: pre;"> $iphoneLocation->accuracy);
// All done.
echo "Done!";
When you visit this script, you should see your iPhone location. So now you can display this data on your page or use Google Maps API to show it on the map.
If you are outdoors, your location is usually shown to a few feet accurate. On the other hand, indoors the GPS signal is weak and this feature is not really usable (accuracy is about 2000 of something - it can be off for about 1000 feet).
Leave a Comment
:iPhone location php
more...
If you have an iPhone and a MobileMe subscription it's very easy to publish your location online.
Firstly you need to turn on "Find My iPhone" function on iPhone in Settings application and you need to make sure you have data pushed to the iPhone.
Then you need a PHP class for fetching your iPhone location through me.com (http://clickontyler.com/blog/2009/06/sosumi-a-mobileme-scraper/).
Code for fetching data from the server:
// Get the iPhone location from MobileMe
echo "Fetching iPhone location...";
$mobileMe = new Sosumi ($mobileMeUsername, $mobileMePassword);
$iphoneLocation = $mobileMe->locate();
echo "got it.";
echo "iPhone location: $iphoneLocation->latitude, $iphoneLocation->longitude";
echo "iPhone accuracy: $iphoneLocation->accuracy";
echo "<br />";
if($iphoneLocation->latitude > 0) {
echo file_get_contents("http://example.com/update-location/?latitude=".$iphoneLocation->latitude."&longitude=".$iphoneLocation->longitude."&accuracy=".$iphoneLocation->accuracy."&key=auth-key");
}
// Now update Google Latitude
//echo "Updating Google Latitude...";
//$google->updateLatitude($iphoneLocation->latitude, $iphoneLocation->longitude,
//<span style="white-space: pre;"> $iphoneLocation->accuracy);
// All done.
echo "Done!";
<?php
include("sosumi.php");
$mobileMe = new Sosumi ("MobileMe User Name", "MobileMe Password");
$iphoneLocation = $mobileMe->locate();
if($iphoneLocation->latitude > 0) {
echo "iPhone location: $iphoneLocation->latitude, $iphoneLocation->longitude\
";
echo "iPhone accuracy: $iphoneLocation->accuracy";
}else{
echo "Error occured while fetching iPhone location data. Please try again later.";
}
?>
When you visit this script, you should see your iPhone location. So now you can display this data on your page or use Google Maps API to show it on the map.
If you are outdoors, your location is usually shown to a few feet accurate. On the other hand, indoors the GPS signal is weak and this feature is not really usable (accuracy is about 2000 of something - it can be off for about 1000 feet).
HTML5 video and audio tag
by someone16 on Jun.27, 2009, under Computers
Hi, what do you think about HTML5's video and audio tag?
Present: Every video site (eg. YouTube) is using Flash player to play video. Almost every podcast site is using Flash player to play audio. Because of that (using Flash in ways in which it was never attended to use) your CPU is under too much load and because of that fans needs to spin faster and battery life is shorter.
Future: Every video/audio web site will be using video/audio tag and videos are going to be played with built-in player. CPU is under normal load and you can watch 1 more hour (approximately) of watching movie/vlog/...
For playing videos through Flash Player, it was necessary to convert it to .flv (Flash Video) file. Latest Flash Player version support .mp4 format (for HD videos). Sites like YouTube must pay companies who owns these formats certain amount of money for using it.
Video tag supports ogg, which is open source and free to use.
Pros and cons of using video tag over Flash Player:
+ less load on CPU (+ longer battery life, + less heat - fans are spinning slower)
+ native support by all operating systems/web browsers (Flash is not fully supported on Linux)
+ easier to create web page which contain video/audio
- no ads and menus (like you see on YouTube and other channels)
- player's design depends on used operating system - owner of site cannot create it's own design and cannot add special functions (you couldn't add links on video like you can now on YouTube)
My point of view: I hope that more and more web browsers will support HTML5 and that designers/programmers will prefer this over Flash Player
Leave a Comment
:Computers flash html5 programming youtube
more...
Present: Every video site (eg. YouTube) is using Flash player to play video. Almost every podcast site is using Flash player to play audio. Because of that (using Flash in ways in which it was never attended to use) your CPU is under too much load and because of that fans needs to spin faster and battery life is shorter.
Future: Every video/audio web site will be using video/audio tag and videos are going to be played with built-in player. CPU is under normal load and you can watch 1 more hour (approximately) of watching movie/vlog/...
For playing videos through Flash Player, it was necessary to convert it to .flv (Flash Video) file. Latest Flash Player version support .mp4 format (for HD videos). Sites like YouTube must pay companies who owns these formats certain amount of money for using it.
Video tag supports ogg, which is open source and free to use.
Pros and cons of using video tag over Flash Player:
+ less load on CPU (+ longer battery life, + less heat - fans are spinning slower)
+ native support by all operating systems/web browsers (Flash is not fully supported on Linux)
+ easier to create web page which contain video/audio
- no ads and menus (like you see on YouTube and other channels)
- player's design depends on used operating system - owner of site cannot create it's own design and cannot add special functions (you couldn't add links on video like you can now on YouTube)
My point of view: I hope that more and more web browsers will support HTML5 and that designers/programmers will prefer this over Flash Player
Windows 7
by someone16 on Jun.27, 2009, under Computers
Hi all,
Yesterday I tried Windows 7 Release Candidate and I have no problems with it. It's good operating system, but I still prefer Mac OS. Microsoft did a good job with this Windows version and hopefully they will sell more copies of it than of Vista.
It would be great if all Windows XP users move to 7, because XP's are 8 years old and don't have support anymore. But this won't gonna happen. I really do not understand people who insist on using that old OS. Even Linux is better (not that I have anything against it).
AFAIK a lot of people moved to Mac/Linux because Vista is a little bit crappy. Windows 7 must make for all lost users because of Vista. Linux can't do much with it's less than 1% of all users. But there is a posibility of Google making OS. Google give away free services, people trust Google. I think it's only company which can overtake Microsoft's Windows market share.
But that is higly unlikely. I watched video on YouTube where they ask some people if they heart of Google Chrome ... 95% didn't even know what that is. So just because Google makes something doesn't mean, that everyone will use it. (They have some sucessfull projects like Google and Gmail).
So Microsoft has nothing to worry about ... at least for now.
Leave a Comment
:Computers operating system windows windows 7 windows xp
more...
Yesterday I tried Windows 7 Release Candidate and I have no problems with it. It's good operating system, but I still prefer Mac OS. Microsoft did a good job with this Windows version and hopefully they will sell more copies of it than of Vista.
It would be great if all Windows XP users move to 7, because XP's are 8 years old and don't have support anymore. But this won't gonna happen. I really do not understand people who insist on using that old OS. Even Linux is better (not that I have anything against it).
AFAIK a lot of people moved to Mac/Linux because Vista is a little bit crappy. Windows 7 must make for all lost users because of Vista. Linux can't do much with it's less than 1% of all users. But there is a posibility of Google making OS. Google give away free services, people trust Google. I think it's only company which can overtake Microsoft's Windows market share.
But that is higly unlikely. I watched video on YouTube where they ask some people if they heart of Google Chrome ... 95% didn't even know what that is. So just because Google makes something doesn't mean, that everyone will use it. (They have some sucessfull projects like Google and Gmail).
So Microsoft has nothing to worry about ... at least for now.
Windows XP versus Windows Vista
by someone16 on Jun.10, 2009, under Computers
A few days ago I did a Super PI test on my old laptop with Windows XP and Windows Vista.
System:
- 1.5 GHz Intel Celeron M
- 512 MB RAM
Both systems were fresh installed with all needed drivers.
Results:
Conclusion: Windows XP did better with small calculations, but Vista beats it at larger calculations (16M and 32M).
Leave a Comment
:super pi windows vista windows xp
more...
System:
- 1.5 GHz Intel Celeron M
- 512 MB RAM
Both systems were fresh installed with all needed drivers.
Results:
Windows Vista Windows XP
+ 000h 00m 02s [ 64K] + 000h 00m 01s [ 64K]
+ 000h 00m 04s [ 128K] + 000h 00m 04s [ 128K]
+ 000h 00m 09s [ 256K] + 000h 00m 09s [ 256K]
+ 000h 00m 24s [ 512K] + 000h 00m 23s [ 512K]
+ 000h 00m 58s [ 1M] + 000h 00m 57s [ 1M]
+ 000h 02m 24s [ 2M] + 000h 02m 21s [ 2M]
+ 000h 05m 23s [ 4M] + 000h 05m 17s [ 4M]
+ 000h 12m 18s [ 8M] + 000h 11m 54s [ 8M]
+ 000h 25m 42s [ 16M] + 000h 25m 52s [ 16M]
+ 000h 53m 33s [ 32M] + 000h 56m 07s [ 32M]
Conclusion: Windows XP did better with small calculations, but Vista beats it at larger calculations (16M and 32M).


