Posts Tagged ‘PHP’
22
Dec

It’s almost the end of the year 2009. This year is special because I’ve never dealt with so many languages in a year. Objective C, Python, Java, C++, PHP, JavaScript, Haskell, Prolog, Ruby, C#, and probably more. Some of them were for work, some for school, and others were for my personal projects. I have plenty of stories for each language, but I’ll try to keep them brief.

Objective C

CQube iPhone edition was released back in March, and as you know Objective C is the language you use for iPhone application development. Objective C is a strict superset of C, so it didn’t take too much effort to learn. Rather, I spent most of time to learn the Cocoa framework. All those magic keywords like @property, @synthesize, and @dynamic were nice to have.

Python

I love Python. I used Python for many different things; from disposable command line utilities to implementation of Apple Push Notification service. I also used this language to conduct a classified operation. After I learned Haskell, which is what I’m going to talk about in a minute, I started enjoying use of higher order functions such as map, reduce, filter, and so on.

Java

I also use Java a lot. I built a Java mobile application at my work. I also built a nearest neighbor search engine, an HTTP based real time communication framework, and a Luca compiler. There are probably more, but these are the ones I remember. There are few things that I really wish to have in Java: automatic getter/setter, operator overriding, and delegate. Could someone give me these as a Christmas gift? ;-)

C++

To be honest with you, I’m kind of scared of this magic language. I would say it’s quite difficult and takes a considerable amount of practice to use this language properly and effectively. Well, in my compiler class, I had to use either C or C++ to implement a Luca interpreter with indirect threading.

PHP

I’m not a big fan of this language. Ironically, this is probably one of the top three languages that I spent most time with so far. I had a web application development job over the summer. It was a lot of PHP programming. I also had to build server side components at my current work. I wanted to use Python, but apparently not many people know Python. So, I had stick with whatever language that most people can deal with.

JavaScript

You would imagine using JavaScript to manipulate the DOM structure of a web page. But, this is not the only case. I used this language to build a Firefox extension (thanks to Justin Samuel’s Firefox extension development tutorial). I can’t tell you what it is though because it’s a part of a classified operation.

Haskell

Haskell changed a lot of things in my point of view of programming. All the functional programming features and its imperativeness really blew my mind. Basically it made me a better developer in general. Even though I have no intention to use Haskell in production environment, I will be using things I learned from this language.

Prolog

Cody Jorgensen:

Why can’t I get an element from the god damn list?

Ruby

It was very joyful to learn Ruby as I already know how to use Python. Ruby is very similar to Python in many different aspects as C# is similar to Java. I see blocks and the yield keyword as a giant lambda function, and I kind of like it. One thing I like from this language over Python is that there’s end keyword so that you don’t have to be super careful about indentation. Because of this, there’s no need to make a new template language in web application framework such as Ruby on Rails, whereas it is necessary in Python based frameworks such as Django. Oh, it’s also very nice to have built-in regular expression features.

C#

We decided to switch from Nokia’s Symbian to Microsoft Windows Mobile platform at my work. I was hoping the Java code I’ve been writing works just fine on the Windows Mobile, but few things didn’t really work out well. So, I decided to re-write the whole application in C#, and I did it over the last weekend. I wouldn’t be able to make all these happen without Visual Studio’s great help. Not to mention I didn’t have to re-write all the server side components.

, , , , , , , ,

19
Jun

Introduction

A timer is a very commonly used feature for various purposes such as unit tests or performance optimization. However, it is such a painful job to implement a timer every time as demanded, even though it takes only few lines of code. Here is a solution for those kind of issues.

Basic Usage

Using Timer is pretty simple. It’s just like a stop watch in the real life. All we need to do is to push the start button as a sprinter does a start dash, and push the stop button as the sprinter finishes.

Timer::start();

// sprint!

Timer::end();
Timer::display();

The following or similar message will be displayed by default if no argument is given at display() method.

This page was created in 0.007 seconds.

Advanced Usage

We can easily change the message by giving an argument.

Timer::display('It has been %.5f sec');

Then it will display the following.

It has been 0.00719 sec

Also, we can change the time unit.

Timer::display('This page was created in %d ms.', 1000);

The second argument mutiplies the duration, which is given in seconds, by itself. As a result, the time unit will be milliseconds instead of seconds.

This page was created in 7 ms.

Similarly, if you put 1,000,000 or even 1,000,000,000 then the time unit will be microseconds and nanoseconds, respectively. However, there is no guarantee that the timer provides such accuracy.

Restrictions

This Timer class is not designed for nested multiple timers on a single page. (i.e. Once a timer starts, we can’t start the timer before we stop the timer. Otherwise, it wouldn’t work properly) However, we can easily change it by removing static keywords, and substituting self keyword with this so that we can generate multiple timers. Also, we can add a constructor as necessary. Now, we’re gonna have to make an actual instance of Timer to be able to use it.

Full Source

<?php
/**
 * @since 20070619
 * @version 20070619
 * @author Sumin Byeon
 * @copyright Copyright (c) 2003-2007 SBBS Team
 */
class Timer {

    private static $start;
    private static $end;

    public static function start() {
        self::$start = microtime(true);
    }

    public static function end() {
        self::$end = microtime(true);
    }

    public static function getDuration() {
        return self::$end - self::$start;
    }

    public static function display($message = 'This page was created in %.3f seconds.<br/>', $multiplier=1) {
        printf($message, self::getDuration()*$multiplier);
    }
}
?>

References

11
Feb

Base62

필요하신 분은 가져다 쓰세요.

class base62 {

    const $charset = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
    const $base = 62;

    public function encode($var) {
        $stack = array();
        while($var > 0) {
            $remainder = $var % self::$base;
            $var = (int)($var / self::$base);

            array_push($stack, self::$charset[$remainder]);
        }

        return implode('', array_reverse($stack));
    }

    public function decode($var) {

        $length = strlen($var);
        $result = 0;
        for($i=0; $i<$length; $i++) {
            $result += self::get_value($var[$i]) * pow(self::$base, ($length-($i+1)));
        }

        return $result;
    }

    private function get_value($var) {
        if(ereg('[0-9]', $var))
            return (int)(ord($var) - ord('0'));
        else if(ereg('[A-Z]', $var))
            return (int)(ord($var) - ord('A') + 10);
        else if(ereg('[a-z]', $var))
            return (int)(ord($var) - ord('a') + 36);
        else
            return $var;
    }
}

_

base62::encode(61);
base62::encode(128);
base62::encode(65536);
base62::encode(2147483647);

base62::decode(base62::encode(51));
base62::decode(base62::encode(255));
base62::decode(base62::encode(33232));
base62::decode(base62::encode(2047343643));

_

z
24
H32
2LKcb1

51
255
33232
2047343643