Thursday, April 14, 2011

Calculate Elapsed Time in C


We can use the function and structs in standard C library (sys/time.h) to calculate the elapsed time value between two time instances:

1. To get the processor time of the program, we use the C library clock_t struct to count the CPU clock ticks, and then divide the CLOCKS_PER_SEC to get the time:

    e.g.
        clock_t t;
        t = clock();   
        // Do something
        t = clock() - t;
        double cpu_time_elapsed = (double)t/CLOCKS_PER_SEC;


2. To calculate the time interval in wall clock time, use the C library time_t struct and the time difference function:
   
    double difftime(time_t end, time_t start)
   
  , where the start and end time can be initialized by calling time_t time(time_t *time) function, and the return value is the seconds (in double) elapsed between start and end.

    e.g.   
        time_t start, end;
        time(&start);
        // Do something
        time(&end);
        double time_elapsed = difftime(end, start);


3. To calculate the time interval more precisely (in higher resolution), we can define a C function like:

    double getTimeElapsed(struct timeval end, struct timeval start)
    {
        return (end.tv_sec - start.tv_sec) + (end.tv_usec - start.tv_usec) / 1000000.00;
    }

  , where the struct timeval is elapsed time declared in the sys/time.h. It contains two members:

        tv_sec (number of seconds elapsed)
       
  and
 
        tv_usec (remaining second fraction in microseconds).

    To get the start and end time value, we call the gettimeofday() function.
 
    e.g.
        timeval start, end;
        gettimeofday(&start, NULL);
        // Do something
        gettimeofday(&end, NULL);
        double time_elapsed = getTimeElapsed(end, start);

       
    Another time struct, timespec has two members tv_sec and tv_nsec, where tv_nsec represent remaining second fraction in nanosecond.
   

Reference: 
1. GNU C Library Manual: 21.2 Elapsed Time
2. difftime on cplusplus.com

Sunday, April 3, 2011

Install Ubuntu 10.10 using wubi on Windows 7 (Avoid Permission Denied Error)

Problem:

Wubi provides a fast and convinient way for us to install Ubuntu on an existing Windows OS. However, when I was installing Ubuntu 10.10 by wubi on Windows 7, I got an error message "An error occured: Permission Denied".

Solution:

To solve this problem, you can do the following:
  1. Download the CD image xxx.iso (e.g. Intel x86 Desktop: ubuntu-10.10-desktop-i386.iso) and save to under your partition (e.g. D:/).
  2. Unmount the image file with either DAEMON tool, WinRAR or 7zip, extract the wubi.exe file to the folder containing the image file.
  3. Run the wubi.exe (and no "permission denied" error will occur!)
  4. You can choose to delete the image file after reboot.
Done and enjoy!

Saturday, April 2, 2011

1. Yii - Framework Installation

# 0. preparation
- LAMP

# 1. create a project from yii:
- cd to the web root and run the follow in the cmd:
 
# 2. config the project
- go to the ./config/main.php
- remove the comment and add your customed password of the generator
'gii'=>array(
'class'=>'system.gii.GiiModule',
'password'=>'[YOURPASSWORD]',
'ipFilters'=>array('127.0.0.1','::1'),
),
 
- also have an application base directory /wwwroot/project_name/protected. It should be protected from being accessed by Web users. For Apache httpd Web server, we place under this directory a .htaccess file with the following content:
 
- set up create and write access to the view, model and controller folder
chmod -777 view -R
chmod -777 controller -R
chmod -777 model -R

- The gii tool should be removed when running on the production machine.
- The debug mode should also be disabled on a production machine.

# 3. then edit the view/greeting page
- localhost/[project]/index.php?r=greeting/index
r = route the request takes the controller id / action id pair
greeting = controller id - tells greeting controller class
index = action id - tells to use the action index in the class
- $this in the view file refers to the controller class which renders it

# 4. config database
- Config the database in the config/main.php and uncomment the mysql
- change the database name, password, username for the mysql
- go to gii "Model Class" to create the model of [modelid]
- name the table prefix, name class.
the table prefix fields will ignore the prefix will choosing the model class

# 5. create the ORM using CRUD module
- go to the crud module and type in the [modelid]
- create the crud object
- go to the link: index.php?r=[modelid]


Reference:
1. http://www.yiiframework.com/doc/guide/1.1/en/quickstart.installation
2. http://www.yiiframework.com/screencasts/


deny from all

yii/framework/yiic webapp [project_name]