--- /dev/null
+runs=10
+
+class Task:
+ name = "unnamed"
+ pre_cmd = ""
+ cmd = ""
+ post_cmd = ""
+ results = 0
+ remain = 10
+
+ def __init__(self):
+ self.results = []
+
+tasks = []
+
+t1 = Task()
+t1.name = "C version (without print)"
+t1.pre_cmd = ""
+t1.cmd = "pushd ../c >/dev/null; ./main; popd >/dev/null;"
+t1.post_cmd = ""
+tasks.append(t1)
+
+t3 = Task()
+t3.name = "C version (with print)"
+t3.pre_cmd = ""
+t3.cmd = "pushd ../c >/dev/null; ./main -p; popd >/dev/null;"
+t3.post_cmd = ""
+tasks.append(t3)
+
+t5 = Task()
+t5.name = "C version (with print, but sent to /dev/null)"
+t5.pre_cmd = ""
+t5.cmd = "pushd ../c >/dev/null; ./main -p >/dev/null; popd >/dev/null;"
+t5.post_cmd = ""
+tasks.append(t5)
+
+t2 = Task()
+t2.name = "Java version (without print)"
+t2.pre_cmd = ""
+t2.cmd = "pushd ../java >/dev/null; java read_trace; popd >/dev/null;"
+t2.post_cmd = ""
+tasks.append(t2)
+
+t4 = Task()
+t4.name = "Java version (with print)"
+t4.pre_cmd = ""
+t4.cmd = "pushd ../java >/dev/null; java read_trace -p; popd >/dev/null;"
+t4.post_cmd = ""
+tasks.append(t4)
+
+t6 = Task()
+t6.name = "Java version (with print, but sent to /dev/null)"
+t6.pre_cmd = ""
+t6.cmd = "pushd ../java >/dev/null; java read_trace -p >/dev/null; popd >/dev/null;"
+t6.post_cmd = ""
+tasks.append(t6)
+
+import os
+import time
+
+def average(lst):
+ sum = 0
+ count = 0
+
+ for i in lst:
+ sum += i
+ count += 1
+
+ if count == 0:
+ return 0
+ else:
+ return sum/count
+
+def min(lst):
+ if len(lst) == 0:
+ return 0
+
+ found = lst[0]
+
+ for i in lst:
+ if i < found:
+ found = i
+
+ return found
+
+def max(lst):
+ if len(lst) == 0:
+ return 0
+
+ found = lst[0]
+
+ for i in lst:
+ if i > found:
+ found = i
+
+ return found
+
+def main():
+ for task in tasks:
+ while task.remain > 0:
+ os.system(task.pre_cmd)
+ t_start = time.time()
+ os.system(task.cmd)
+ t_end = time.time()
+ os.system(task.post_cmd)
+ task.remain-=1
+ task.results.append(t_end-t_start)
+ print(task.results)
+
+ print "------------------------------------"
+ for task in tasks:
+ print "RESULTS for " + task.name
+ print "Runs: " + str(len(task.results))
+ print "Average: " + str(round(average(task.results), 3))
+ print "Min: " + str(round(min(task.results), 3))
+ print "Max: " + str(round(max(task.results), 3))
+ print ""
+
+main()
--- /dev/null
+main: main.c
+ gcc -o main -O3 main.c
+
+clean:
+ rm main
--- /dev/null
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <unistd.h>
+
+#include <stdio.h>
+#include <stdlib.h>
+
+int main()
+{
+ int result;
+ int fd;
+
+ result = fd = open("../trace.dat", O_RDONLY);
+ if(result == -1) {
+ perror("open");
+ return 1;
+ }
+
+ while(1) {
+ unsigned long timestamp;
+ unsigned short id;
+ unsigned char arglen;
+ char *args;
+
+ result = read(fd, ×tamp, 4);
+ if(result == 0)
+ break;
+ if(result < 4) {
+ perror("read");
+ return 1;
+ }
+
+ result = read(fd, &id, 2);
+ if(result < 2) {
+ perror("read");
+ return 1;
+ }
+
+ result = read(fd, &arglen, 1);
+ if(result < 1) {
+ perror("read");
+ return 1;
+ }
+
+ args = malloc(arglen);
+
+ result = read(fd, args, arglen);
+ if(result < arglen) {
+ perror("read");
+ return 1;
+ }
+
+ unsigned short arg1;
+ char *arg2;
+
+ arg1 = *(unsigned short *)args;
+ arg2 = args+2;
+
+ printf("timestamp %u id %hu args=(arg1=%hu arg2=\"%s\")\n", timestamp, id, arg1, arg2);
+
+ free(args);
+ }
+ close(fd);
+
+ return 0;
+}
--- /dev/null
+read_trace.class: read_trace.java
+ javac -source 1.5 read_trace.java
--- /dev/null
+import java.io.*;
+
+public class read_trace {
+ public static void main(String[] args) {
+ int tmp = 0;
+
+ try {
+ FileInputStream file_input = new FileInputStream ("../trace.dat");
+ DataInputStream data_input = new DataInputStream (file_input);
+
+ int timestamp;
+ short id;
+ byte length;
+
+ while(true) {
+ timestamp = data_input.readInt();
+ id = data_input.readShort();
+ length = data_input.readByte();
+
+ byte[] buf = new byte[length];
+ data_input.readFully(buf, 0, length);
+
+
+ ByteArrayInputStream eargs = new ByteArrayInputStream(buf);
+ DataInputStream eargs_data = new DataInputStream (eargs);
+
+ /* read arg 1 (short) */
+ short arg1 = eargs_data.readShort();
+
+ /* read arg 2 (string) */
+ eargs_data.mark(10000);
+ int strlen=0;
+ while(eargs_data.readByte() != 0)
+ strlen++;
+ eargs_data.reset();
+ byte[] arg2 = new byte[strlen];
+ eargs_data.readFully(arg2, 0, strlen);
+
+ //System.out.printf("timestamp %d id %d args=(short=%d string=\"%s\") %n", timestamp, id, arg1, new String(arg2));
+ String strout = String.format("timestamp %d id %d args=(short=%d string=\"%s\") %n", timestamp, id, arg1, new String(arg2));
+ tmp += strout.length();
+ }
+
+ }
+ catch(IOException e) {
+ //System.out.println ("IO exception = " + e );
+ //e.printStackTrace();
+ }
+
+ //file_input.close();
+ System.out.println(tmp);
+ }
+}
--- /dev/null
+(for i in $(seq 1000); do cat basic_record.dat; done) >tmp.dat
+(for i in $(seq 10); do cat tmp.dat; done) >trace.dat