Search

Wednesday, January 7, 2009

Compile C++ code in ubuntu

One of many question of user who switch from window to linux to have how to compile c/c++ source code. Most of study c or c++ at school or home an are usually use windows.

I think simple to start compile c/c++ in ubuntu to use first an editor like nano and create a source file then compile using gcc in command line. but first to install gcc complier and survival other utilities for compile software. use .

sodu apt-get install build-essential

build-essential is a meta package - a package which only depends on other packages, so installing it will automatically install several tools like gcc, g++ or make.

Next, create your source file using a text editor of choice (I used Nano for this example):

nano main.c

Enter the content, e.g.:

#include

int main ()
{
printf ("Hello, world!\n");
return 0;
}


Notice that I also included a newline after the close bracket, otherwise the compiler will issue a warning. Save it with CTRL+O, then exit Nano using CTRL+X. To compile your source, simply use:

gcc main.c -o myapp

The output, myapp, will automatically be executable, so to run it use:

ubuntu@ubuntu:~$ ./myapp
Hello, world!

This is the simplest way of creating and compiling C or C++ code.

Monday, January 5, 2009

Speed Up Mysql query cache size

One of the best ways to speed up your web application is to enable query caching in your database, which caches commonly used SQL queries in memory for virtually instant access by the next page that makes the same request.

The reason this method is so powerful is that you don't have to make any changes to your web application, you just have to sacrifice a little bit of memory. This isn't going to fix all of your problems, but it definitely can't hurt.

Note: if your application updates tables frequently, then the query cache will be constantly purged and you won't get much or any benefit from this. This is ideal for an application that mostly does reads against the database, such as a Wordpress blog. This also won't work if you are running on shared hosting.

Enable Caching with Server Running

The first thing you'll want to do is make sure that your installation of MySQL actually has query caching support available. Most distributions do, but you should check anyway.

You'll want to run this command from your MySQL console, which will tell you if query caching is available.

mysql> show variables like 'have_query_cache';
+------------------+-------+
| Variable_name | Value |
+------------------+-------+
| have_query_cache | YES |
+------------------+-------+

Don't mistake this as meaning that query caching is actually enabled, because most hosting providers aren't going to enable this by default. Oddly enough, my Ubuntu Feisty installation already had it enabled…

Next we'll need to check and see if query caching is enabled. We'll need to check more than one variable, so we may as well do it all at once by checking for the variable query%

mysql> show variables like 'query%';
+------------------------------+---------+
| Variable_name | Value |
+------------------------------+---------+
| query_alloc_block_size | 8192 |
| query_cache_limit | 1048576 |
| query_cache_min_res_unit | 4096 |
| query_cache_size | 8388608 |
| query_cache_type | ON |
| query_cache_wlock_invalidate | OFF |
| query_prealloc_size | 8192 |
+------------------------------+---------+

Here's the important items in the list and what they mean:

* query_cache_size - This is the size of the cache in bytes. Setting this value to 0 will effectively disable caching.
* query_cache_type - This value must be ON or 1 for query caching to be enabled by default.
* query_cache_limit - This is the maximum size query (in bytes) that will be cached.

If the query_cache_size value is set to 0 or you just want to change it, you'll need to run the following command, keeping in mind that the value is in bytes. For instance, if you wanted to allocate 8MB to the cache we'd use 1024 * 1024 * 8 = 8388608 as the value.

SET GLOBAL query_cache_size = 8388608;

Similarly, the other options can be set with the same syntax:

SET GLOBAL query_cache_limit = 1048576;

SET GLOBAL query_cache_type = 1;

Now how do we tell if it's actually working? You can use the SHOW STATUS command to pull all the variables that start with "Qc" to take a look at what is going on under the hood.

mysql> SHOW STATUS LIKE 'Qc%';
+-------------------------+--------+
| Variable_name | Value |
+-------------------------+--------+
| Qcache_free_blocks | 65 |
| Qcache_free_memory | 201440 |
| Qcache_hits | 18868 |
| Qcache_inserts | 2940 |
| Qcache_lowmem_prunes | 665 |
| Qcache_not_cached | 246 |
| Qcache_queries_in_cache | 492 |
| Qcache_total_blocks | 1430 |
+-------------------------+--------+
8 rows in set (0.00 sec)

You'll notice in the stats that I have plenty of free memory left. If your server shows a lot of lowmem prunes, you might need to consider increasing this value, but I wouldn't spend too much memory on query caching for a web server… you need to leave memory available for apache, php, ruby, or whatever you are using.

Enable in Config File

If you want these changes to survive a reboot or restart of the mysql server, you'll need to add them into your /etc/mysql/my.cnf configuration file for MySQL. Note that it might be in a different location on your installation.

Open up the file using a text editor in sudo or root mode, and then add these values if they don't already exist in the file. If they do exist, just uncomment them.

query_cache_size = 268435456
query_cache_type=1
query_cache_limit=1048576

Query caching can significantly improve the speed of your web application, especially if your application does mostly reads. Monitor the status using the methods above and see how it works over time.

link : http://www.howtogeek.com/howto/programming/speed-up-your-web-site-with-mysql-query-caching/

Thursday, December 18, 2008

addslashes single quote and double quote

<?php
$old1 = 'H"s';
$old2 = "H's";
echo "new1 : " . addslashes ($old1);
echo "
";
echo "new2 : " . addslashes ($old2);
? >

The output :
new1 : H\"s
new2 : H\'s

Sunday, December 14, 2008

Spanning Tree Example

http://www.cisco.com/warp/public/473/spanning_tree1.swf

Finding prim number


#include < stdio.h>
#include < conio.h>
#include < math.h>
main()
{
int A,B,C=0,D;
printf("Program for finding Prime Number!\n\n\t you enter D=");
scanf("%d",&D);
printf("\nSo, prime number which is less than %d is...\n\n ",D);
printf("2\n");
for(A=3;A<=D;A+=2) { for(B=3;B<=sqrt(A);B+=2) {if(A%B==0) break;} if(B>sqrt(A))
{ printf("%d\n",A);
C+=1;
}

}

printf("\n\n\t***There are %d.\n\n\n\r",C+1);
getch();
}

Relational Database Normalization Examples 7/7

ตัวอย่าง 5.1 ตารางที่ยังไม่ทำ normalization (จากเอกสารของผู้ใช้ที่ส่งให้นักวิเคราะห์)
เป็นตารางการสั่งซื้อ ให้ท่านลองนำไปทำ normalization
orderid
301
orderdate
15/12/46
productid
401
402
quan
2
5
302 16/12/46 402
405
406
4
4
1
ตัวอย่าง 5.2 ตารางที่ยังไม่ทำ normalization (จากเอกสารของผู้ใช้ที่ส่งให้นักวิเคราะห์)
เป็นตารางเงินเดือน ให้ท่านลองนำไปทำ normalization
saleid
101




salesalary
2000
saleposition
sale
custid
201
202
203
204
custname
tom
dang
boy
girl
ตัวอย่าง 5.3 ใบเสร็จ 2 ใบ เลขที่ A0001 และ A0002 (ให้ท่านลองนำไปทำ normalization)
ตัวอย่างนี้ได้รับการเสนอแนะจาก sudomvon@minebea.co.th
                              Invoice No.:      A0001
Date: 24/10/2004
Sold to: ABC company
Item Quantity UnitPrice Amount
Pen 50 3 150
Book 100 5 500
Ruler 20 2 40
Total 690


Invoice No.: A0002
Date: 24/10/2004
Sold to: XYZ company
Item Quantity UnitPrice Amount
Pen 100 3 300
Book 120 5 600
Ruler 50 2 100
Total 1000
การทำ Normalization ของตัวอย่าง 5.3 (ได้รับคำแนะนำจาก อ.อมรทิพย์ rung@yonok.ac.th)
    การเริ่มต้นทำ normalization ต้องนำข้อมูลมาสร้าง ตารางเริ่มต้น ซึ่งเป็นขั้นตอนก่อนการทำ 1NF
    โดยไม่เขียน amount และ total เพราะเป็นค่าที่เกิดจากการคำนวณเท่านั้น
    ตาราง temp
    A0001,24/10/2004,ABC company,Pen,50,3
    A0001,24/10/2004,ABC company,Book,100,5
    A0001,24/10/2004,ABC company,Ruler,20,2
    A0002,24/10/2004,XYZ company,Pen,100,3
    A0002,24/10/2004,XYZ company,Book,120,5
    A0002,24/10/2004,XYZ company,Ruler,50,2

    การทำ 1NF ของ ตัวอย่าง 5.3
    นำตารางเริ่มต้นมาจัดการเรื่อง multivalue เช่น คนชอบกินหลายอย่าง หรือการเพิ่ม key ประกอบข้อมูลที่อาจซ้ำ
    ตัวอย่างนี้เพิ่มรหัสลูกค้า และรหัสสินค้า เป็น key เพราะคิดว่า ชื่อลูกค้า และชื่อสินค้า อาจมีโอกาสซ้ำกันได้ และไม่เหมาะที่จะเป็น key
    ตาราง tempoforder
    A0001,24/10/2004,101,ABC company,201,Pen,50,3
    A0001,24/10/2004,101,ABC company,202,Book,100,5
    A0001,24/10/2004,101,ABC company,203,Ruler,20,2
    A0002,24/10/2004,102,XYZ company,201,Pen,100,3
    A0002,24/10/2004,102,XYZ company,202,Book,120,5
    A0002,24/10/2004,102,XYZ company,203,Ruler,50,2
    การทำ 2NF ของ ตัวอย่าง 5.3
    แยกตาราง tempoforder เป็นหลายตาราง และทุกตารางมี primary key ที่มีการตรวจสอบการขึ้นตรงอย่างถูกต้อง
    primay key คือ รหัสใบสั่งซื้อ
    ตาราง invoid_1
    A0001,24/10/2004,101,ABC company
    A0002,24/10/2004,102,XYZ company
    primay key คือ รหัสใบสั่งซื้อ และรหัสสินค้า
    ตาราง invoid_2
    A0001,201,50,3
    A0001,202,100,5
    A0001,203,20,2
    A0002,201,100,3
    A0002,202,120,5
    A0002,203,50,2
    ตาราง product เพราะราคาขึ้นกับรหัสสินค้า
    primary key คือ รหัสสินค้า

    ตาราง product
    201,Pen,3
    202,Book,5
    203,Ruler,2

    การทำ 3NF ของ ตัวอย่าง 5.3
    ตาราง invoid_1 เพราะชื่อลูกค้าไม่ขึ้นกับรหัสใบสั่งซื้อ จึงต้องแยกไปทำตาราง customer
    primay key คือ รหัสใบสั่งซื้อ

    ตาราง invoid_1
    A0001,24/10/2004,101
    A0002,24/10/2004,102
    ตาราง customer เพราะชื่อลูกค้าไม่ขึ้นกับ รหัสใบสั่งซื้อ แต่ขึ้นกับรหัสลูกค้า
    primay key คือ รหัสลูกค้า

    ตาราง customer
    101,ABC company
    102,XYZ company
    primay key คือ รหัสใบสั่งซื้อ และรหัสสินค้า เพราะ ปริมาณ และราคาต้องขึ้นกับ key 2 ตัวนี้
    ตาราง invoid_2
    A0001,201,50,3
    A0001,202,100,5
    A0001,203,20,2
    A0002,201,100,3
    A0002,202,120,5
    A0002,203,50,2
    ตาราง product เพราะราคาขึ้นกับรหัสสินค้า
    primary key คือ รหัสสินค้า

    ตาราง product
    201,Pen,3
    202,Book,5
    203,Ruler,2

Relational Database Normalization 6/7

ขั้นตอนการ Normalization
    5.1 เปลี่ยนตารางที่ยังไม่เคย Normalization เป็น First Normal Form หรือ 1NF
    วิธีพิจารณา คือ แยกข้อมูลเป็นระเบียน โดยไม่มีการจัดกลุ่ม
    ตารางที่ไม่ผ่านการ normalization
    orderid
    305
    orderdate
    31/01/47
    productid
    432
    455
    467
    quan
    20
    2
    1
    ตารางที่ผ่านการ normalization ในระดับ 1NF
    orderid orderdate productid quan
    305 31/01/47 432 20
    305 31/01/47 455 2
    305 31/01/47 467 1


    5.2 เปลี่ยนจาก 1NF เป็น 2NF(Second Normal Form) คือการเปลี่ยนตารางที่มีปัญหา 4 ประการ
    วิธีพิจารณาเปลี่ยน 1NF เป็น 2NF คือ ไม่มี non key ตัวใด ไม่สัมพันธ์กับ primary key (ให้พิจารณาเฉพาะ non key และ primary key)
    สิ่งที่ได้จากตาราง 1 ตาราง จะแตกออกมาเป็นหลายตาราง
    5.2.1 แก้ไขข้อมูล ต้องแก้หลายระเบียน
    5.2.2 มีข้อมูลเดียวกันในหลายระเบียน อาจขัดแย้งกันได้
    5.2.3 การเพิ่มข้อมูลทำได้ยาก
    5.2.4 การลบข้อมูลทำได้ยาก
    ท่านลองพิจารณา Schema ของตารางนี้ว่าจะแยกได้กี่ตาราง
    จาก orderid,custid,custname,date,proid,proname,price,quantity,categoryid,categoryname
    เป็น orders (orderid,custid,custname,date,proid,proname,price,quantity)
    เป็น categories (categoryid,categoryname)
    เหตุที่แยก categories ออกมา เพราะ categoryid ไม่สัมพันธ์กับ orderid แต่สัมพันธ์กับ proid โดยตรง จึงต้องแยกออกมา

    5.3 เปลี่ยนจาก 2NF เป็น 3NF(Third Normal Form) คือแก้ปัญหายังไม่หมด อาจยังมีข้อมูลที่มีปัญหาอีก จึงต้องทำ 3NF
    วิธีพิจารณาเปลี่ยน 2NF เป็น 3NF คือ ไม่มี non key ตัวใด ขึ้นอยู่กับ non key ตัวอื่นใน entity เดียวกัน (ให้พิจารณาเฉพาะ non key และ non key)
    จาก orders (orderid,custid,custname,date,proid,proname,price,quantity)
    เป็น orders (orderid,custid,date)
    เป็น customers (custid,custname)
    เป็น order details (orderid,proid,price,quantity)
    เป็น products (proid,proname)