[Performance] Memory allocation and array structure


In most of the modern programming language I have learnt, such as C++ and Python, as well as Javascript, the memory allocation for array data structure is like this:

/* (e.g.) */ var a = [["a", "b", 'c"], [1, 2, 3], ["甲", "乙", "丙"]] //Java-script
// /* Memory allocation in order  */ "a" -->  'b" --> "c" --> 1 --> 2 --> 3 --> "甲" --> "乙" --> "丙"

Little thought

Suppose I am gonna store the following sets of data into an array, what’s the most best way to make it accessible most efficiently?

Name No Index
Peter

1

0

Mary

5

1

Lily

30

2

Jonathan 

25

3

Given that the items can be accessed by knowing the index, I have thought of 2 ways of storage

Method 1: The traditional way

var data = [["Peter", 1], ["Mary", 5], ["Lily", 30], ["Jonathan", 25]];
// /* Memory storage: */ "Peter" --> 1 --> "Mary" --> 5 --> "Lily" --> 30 --> "Jonathan" --> 25

This way is very clear and easy to understand, but may not be quite efficient to access the {No} of each element. For instance, the computer has to read pass all {Name}s of each item, and the memory access footprint is long.

Method 2: The “Split" way

var data = [["Peter", "Mary", "Lily", "Jonathan"], [1, 5, 30, 25]];
// /* Memory storage: */ "Peter" --> "Mary" --> "Lily" --> "Jonathan" --> 1 --> 5 --> 30 --> 25

To loop through all {No}s of all data items, just loop within data[1], and the memory access footprint is limited to number of data items.

To access {Name}s and {No}s, just need data[0][x] and data[1][x], where “x" is the index of the item.

There may be a problem associated with this kind of array structure, such as deleting a datum element, it has to loop through different parts of the data (can be understood as “columns" of data in a table). Nonetheless, for storing rare-updated data, I prefer the “split" way.

《Megaman Escape》Python 小遊戲


Megaman Escape (Alpha)

開發背景

這個是我的大學一年級的一個電腦Course其中一份Assignment。Assignment 的要求是讓玩家控制自定主題中的角色(我的主題是【洛克人】,所以主角就是是洛克人了)。首先選擇了關卡的難度(包括敵人的數目,和敵人的最高速度),然後就可以開始遊戲了。目的就是避過從視窗右邊衝過來的敵人。

控制按鈕

遊戲簡介

遊戲背景

目的就是控制洛克人取得最高的分數,一旦離開遊戲,分數會自動給被記錄到外部文件【ranking.txt】。

控制方法

開始遊戲后,點按主角并拖行滑鼠就能移動你的人物。由於開發者對這個遊戲的熱衷,追加了:

  • 【發炮】(按 ”a”制發炮,每一炮的成本是 $150),剩餘彈藥數目會自動更新
  • 【核爆】(按 “s”發動核彈攻擊,畫面上的敵人都會一並給消滅,每一枚核彈的成本為$1000),剩餘彈藥數目會自動更新
  • 如果想隨時退出(按 “m” 返回主菜單)
發炮功能,一按發動

得分方法

  1. 讓敵人通過畫面的左邊,就會有$100進賬
  2. 用【激光炮】攻擊一個敵人,就有$90進賬(爲了減低暴力主義,哈哈-_-|||)
  3. 用【核彈】掃敵,分數視乎畫面的敵人數目,消滅一個敵人有$30進賬(擺明不划算)

如果大家想自爽,可以下載遊戲源代碼,自行修改以下項目:

  • Z = 100 (敵人通過的分數,預設為”100”)
  • laser_kill_pt = 90 (【激光炮】分數進賬,預設為”90”)
  • blast_kill_pt = 30 (【核彈】分數進賬,預設為 “30”)
  • laser_cost = 150 (【激光炮】成本,預設為 “150”)
  • blast_cost = 1000 (【核彈】成本,預設為 “1000”)

閱讀更多»

First joy with Python – Turtle Graphics


If I can still remember, Turtle Graphics has been taught by my D&T (Design and Technology) teacher at secondary school. I thought it was stupid and the GUI is ugly. I knew nothing about programming, and certainly got very little thing to play with Turtle Graphics. I moved it “forward", “rotate" and nothing else.

In Python, Turtle is in 2D space and represented by a Triangle (What?)

But now, I am studying Python and find it rocks. It rocks with its easy-to-program feature, it rocks with tons of modules, and it rocks with Turtle! I have absolutely no pain in studying Python and programming with it. It’s not a language for programming geeks, but also for the newbies. Every statements are just like natural language, (Python is a so-high level programming language). Among its differences from other common programming language in the aspect of statements, its indention-as-structure feature is the most astounding.

Indentation can be used as building up the program structure? I thought it was just for easy reading for programmers, but in Python, it replaces the “BEGIN…END" in Pascal, and “{…}" brackets in C language. WOW! What’s more? I only need the colon to tell Python where I starts a statement section.

Turtle Graphics – Drawing an eye

2 Eyes actually →.→

(Very simple program with no efficiency taken into consideration)

As I have just began my study in Python, this post will be follow by fun updates in Python.

Great Sites for learning Python

  1. http://learnpythonthehardway.org/book/
  2. http://docs.python.org/3.3/tutorial/index.html

閱讀更多»