2018年11月28日 星期三

four design examples for the add function as class or instance methods

一個運算的方法到底要定義為類別方法物件方法,完全取決於設計者。
底下以簡單的整數加法運算為例,說明定義為類別方法或物件方法時,製作及使用上的差別。
一般是類別設計者先決定運算要如何使用,再決定如何製作。

四種設計關鍵在於:  類別方法或物件方法 vs 結果回傳或覆蓋第1數

(1) 類別方法,回傳結果

 public class WholeNumber
 {
    public int number;

    public WholeNumber(int n)
    {
      this.number = n;
    }

    public String toString()
    {
      return String.format("%d", number);
    }

    public static WholeNumber add(WholeNumber n1, WholeNumber n2)
    {
      WholeNumber n = new WholeNumber(0);
      n.number = n1.number + n2.number;
      return n;
    }

    // n3:7 = n1:3 + n2:4
    public static void main(String args[])
    {
      WholeNumber n1 = new WholeNumber(3);
      WholeNumber n2 = new WholeNumber(4);
      WholeNumber n3 = WholeNumber.add(n1, n2);
      System.out.printf("n3:%s = n1:%s + n2:%s\n", n3, n1, n2);
    }
  } 


(2) 類別方法,結果覆蓋第1數

 public class WholeNumber
 {
    public int number;

    public WholeNumber(int n)
    {
      this.number = n;
    }

    public String toString()
    {
      return String.format("%d", number);
    }

    public static void add(WholeNumber n1, WholeNumber n2)
    {
      WholeNumber n = new WholeNumber(0);
      n.number = n1.number + n2.number;
      n1.number = n.number;
    }

    // n1:7 = n1:3 + n2:4
    public static void main(String args[])
    {
      WholeNumber n1 = new WholeNumber(3);
      WholeNumber n2 = new WholeNumber(4);
      WholeNumber n1_copy = new WholeNumber(3);
      WholeNumber.add(n1, n2);
      System.out.printf("n1:%s = n1:%s + n2:%s\n", n1, n1_copy, n2);
    }
  } 


(3) 物件方法,回傳結果

 public class WholeNumber
 {
    public int number;

    public WholeNumber(int n)
    {
      this.number = n;
    }

    public String toString()
    {
      return String.format("%d", number);
    }

    public WholeNumber add(WholeNumber n2)
    {
      WholeNumber n = new WholeNumber(0);
      n.number = this.number + n2.number;
      return n;
    }

    // n3:7 = n1:3 + n2:4
    public static void main(String args[])
    {
      WholeNumber n1 = new WholeNumber(3);
      WholeNumber n2 = new WholeNumber(4);
      WholeNumber n3 = n1.add(n2);
      System.out.printf("n3:%s = n1:%s + n2:%s\n", n3, n1, n2);
    }
 } 


(4) 物件方法,結果覆蓋第1數

 public class WholeNumber
 {
    public int number;

    public WholeNumber(int n)
    {
      this.number = n;
    }

    public String toString()
    {
      return String.format("%d", number);
    }

    public void add(WholeNumber n2)
    {
      WholeNumber n = new WholeNumber(0);
      n.number = this.number + n2.number;
      this.number = n.number;
    }

    // n1:7 = n1:3 + n2:4
    public static void main(String args[])
    {
      WholeNumber n1 = new WholeNumber(3);
      WholeNumber n2 = new WholeNumber(4);
      WholeNumber n1_copy = new WholeNumber(3);
      n1.add(n2);
      System.out.printf("n1:%s = n1:%s + n2:%s\n", n1, n1_copy, n2);
    }
 }

以上四種寫法也可以合併成一支如下類別,方便比較四種用法的不同:

 class WholeNumber
 {
    public int number;

    public WholeNumber(int n)
    {
      this.number = n;
    }
    
    public WholeNumber(WholeNumber n)
    {
      this.number = n.number;
    }

    public String toString()
    {
      return String.format("%d", number);
    }

    public static WholeNumber classAddReturn(WholeNumber n1, WholeNumber n2)
    {
      WholeNumber n = new WholeNumber(0);
      n.number = n1.number + n2.number;
      return n;
    }

    public static void classAddOverwrite(WholeNumber n1, WholeNumber n2)
    {
      WholeNumber n = new WholeNumber(0);
      n.number = n1.number + n2.number;
      n1.number = n.number;
    }

    public WholeNumber instanceAddReturn(WholeNumber n2)
    {
      WholeNumber n = new WholeNumber(0);
      n.number = this.number + n2.number;
      return n;
    }

    public void instanceAddOverwrite(WholeNumber n2)
    {
      WholeNumber n = new WholeNumber(0);
      n.number = this.number + n2.number;
      this.number = n.number;
    }
/*
n3:7 = n1:3 + n2:4
n1:7 = n1:3 + n2:4
n4:7 = n1:3 + n2:4
n1:7 = n1:3 + n2:4
*/
    public static void main(String args[])
    {
      WholeNumber n1,n2,n3,n4,n1_copy;
      
      n1 = new WholeNumber(3);
      n2 = new WholeNumber(4);
      
      n3 = WholeNumber.classAddReturn(n1, n2);
      System.out.printf("n3:%s = n1:%s + n2:%s\n", n3, n1, n2);
      
      n1_copy = new WholeNumber(n1);
      WholeNumber.classAddOverwrite(n1_copy, n2);
      System.out.printf("n1:%s = n1:%s + n2:%s\n", n1_copy, n1, n2);
      
      n4 = n1.instanceAddReturn(n2);
      System.out.printf("n4:%s = n1:%s + n2:%s\n", n4, n1, n2);

      n1_copy = new WholeNumber(n1);
      n1_copy.instanceAddOverwrite(n2);
      System.out.printf("n1:%s = n1:%s + n2:%s\n", n1_copy, n1, n2);
    }
 } 

Virtual Machine Spec for Google Colab Environment

Google Colab提供最多12小時連線的Jupyter Notebook開發環境,其2018年底層的虛擬機實測規格如下:

CPU: Intel(R) Xeon(R) TwinCore @ 2.20GHz x 2

Memory: 13GB

Drive: 347GB

GPU: Tesla K80 with 4992 cores at 556MHz + 11GB Memory

OS: Ubuntu 18.04.1 LTS

Time Limit: 12 hours

規格實測的python指令如下:

# https://stackoverflow.com/questions/48750199/google-colaboratory-misleading-information-about-its-gpu-only-5-ram-available
# memory footprint support libraries/code
!ln -sf /opt/bin/nvidia-smi /usr/bin/nvidia-smi
!pip install gputil
!pip install psutil
!pip install humanize
import psutil
import humanize
import os
import GPUtil as GPU
GPUs = GPU.getGPUs()
# XXX: only one GPU on Colab and isn? guaranteed
gpu = GPUs[0]
def printm():
 process = psutil.Process(os.getpid())
 print("Gen RAM Free: " + humanize.naturalsize( psutil.virtual_memory().available ), " | Proc size: " + humanize.naturalsize( process.memory_info().rss))
 print("GPU RAM Free: {0:.0f}MB | Used: {1:.0f}MB | Util {2:3.0f}% | Total {3:.0f}MB".format(gpu.memoryFree, gpu.memoryUsed, gpu.memoryUtil*100, gpu.memoryTotal))
printm()

!df
!cat /etc/issue
!nvidia-smi
!nvidia-smi -L

!cat /proc/cpuinfo
!cat /proc/meminfo