Skip to content

Commit

Permalink
Merge pull request #1 from skkuse/temporal_green_pattern
Browse files Browse the repository at this point in the history
Merge green pattern
  • Loading branch information
Sunwoo0110 authored Nov 29, 2023
2 parents 0748b23 + 205d6d1 commit 2ce7773
Show file tree
Hide file tree
Showing 299 changed files with 2,250 additions and 0 deletions.
2 changes: 2 additions & 0 deletions green_pattern/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*.class
__pycache__/
Binary file added green_pattern/binary_search/after_12.jar
Binary file not shown.
Binary file added green_pattern/binary_search/before_12.jar
Binary file not shown.
25 changes: 25 additions & 0 deletions green_pattern/checkSubmission.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

import time
import subprocess
import warnings

warnings.filterwarnings("ignore", category=DeprecationWarning)

from os import listdir
from os.path import isfile, join

jar_files = [f for f in listdir("./submission/") if isfile(join("./submission/", f))]

for jar_name in jar_files:
execution_result = subprocess.run(["java", "-jar", f"{jar_name}"], capture_output=True, cwd=f'./submission')
try:
execution_result.check_returncode()
except subprocess.CalledProcessError as err:
print(f"*******{jar_name} runtime error*******")
raw_err = execution_result.stderr
err = raw_err.decode("unicode_escape").strip()
print(err)
exit(0)

print("===All files executed successfully===")
print("======You are ready to submit!!======")
19 changes: 19 additions & 0 deletions green_pattern/compileAll.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import subprocess

from os import listdir
from os.path import isfile, join

patterns = [f for f in listdir("./patterns/") if not isfile(join("./patterns/", f))]

for dir_name in patterns:
bef_compilation_result = subprocess.run(["javac", "Before.java"], capture_output=True, cwd=f'./patterns/{dir_name}')
aft_compilation_result = subprocess.run(["javac", "After.java" ], capture_output=True, cwd=f'./patterns/{dir_name}')
try:
bef_compilation_result.check_returncode()
aft_compilation_result.check_returncode()
except subprocess.CalledProcessError as err:
bef_raw_err = bef_compilation_result.stderr
aft_raw_err = aft_compilation_result.stderr
err = bef_raw_err.decode("unicode_escape").strip() + "\n" + aft_raw_err.decode("unicode_escape").strip()
print(err)
exit(0)
27 changes: 27 additions & 0 deletions green_pattern/compressAll.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import subprocess

from os import listdir
from os.path import isfile, join

patterns = [f for f in listdir("./patterns/") if not isfile(join("./patterns/", f))]

for dir_name in patterns:
srcfiles = listdir(f"./patterns/{dir_name}")

rmfiles = ["After.java", "Before.java", "After.class", "Before.class", "after.jar", "before.jar"]
for file in rmfiles:
if file in srcfiles:
srcfiles.remove(file)

bef_compression_result = subprocess.run(["jar", "cfe", "before.jar", "Before", "Before.class", *srcfiles], capture_output=True, cwd=f'./patterns/{dir_name}')
aft_compression_result = subprocess.run(["jar", "cfe", "after.jar", "After", "After.class", *srcfiles], capture_output=True, cwd=f'./patterns/{dir_name}')
try:
bef_compression_result.check_returncode()
aft_compression_result.check_returncode()
except subprocess.CalledProcessError as err:
bef_raw_err = bef_compression_result.stderr
aft_raw_err = aft_compression_result.stderr
err = bef_raw_err.decode("unicode_escape").strip() + "\n" + aft_raw_err.decode("unicode_escape").strip()
print(err)
exit(0)

49 changes: 49 additions & 0 deletions green_pattern/evaluateAll.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@

import time
import subprocess
import warnings

warnings.filterwarnings("ignore", category=DeprecationWarning)

from os import listdir
from os.path import isfile, join

patterns = [f for f in listdir("./patterns/") if not isfile(join("./patterns/", f))]

for dir_name in patterns:
# measure Before.java execution time

bef_execution_time = 0
start_time = time.perf_counter()
execution_result = subprocess.run(["java", "-jar", "before.jar"], capture_output=True, cwd=f'./patterns/{dir_name}')
bef_execution_time += (time.perf_counter()-start_time)
try:
execution_result.check_returncode()
except subprocess.CalledProcessError as err:
raw_err = execution_result.stderr
err = raw_err.decode("unicode_escape").strip()
print(err)
# some other error handlings
exit(0)

# measure After.java execution time
aft_execution_time = 0
start_time = time.perf_counter()
execution_result = subprocess.run(["java", "-jar", "after.jar"], capture_output=True, cwd=f'./patterns/{dir_name}')
aft_execution_time += (time.perf_counter()-start_time)
try:
execution_result.check_returncode()
except subprocess.CalledProcessError as err:
raw_err = execution_result.stderr
err = raw_err.decode("unicode_escape").strip()
print(err)
# some other error handlings
exit(0)

print(f"============= Result ({dir_name}) =============")
print(f"[Before.jar] runtime: {bef_execution_time:0.4f}s")
print(f"[After.jar ] runtime: {aft_execution_time:0.4f}s")
if bef_execution_time > aft_execution_time:
print(f"=> runtime decreased by {bef_execution_time-aft_execution_time:0.4f}s")
else:
print("=> runtime has not decreased...")
5 changes: 5 additions & 0 deletions green_pattern/pattern_template/After.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
public class After {
public static void main(String[] args){
// write after code
}
}
5 changes: 5 additions & 0 deletions green_pattern/pattern_template/Before.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
public class Before {
public static void main(String[] args){
// write before code
}
}
18 changes: 18 additions & 0 deletions green_pattern/patterns/Arrays_parallelsort/After.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import java.util.Arrays;
import java.util.Random;

public class After {

public static void main(String[] args) {
for (int k = 0 ; k < 1000 ; k++){
int[] data = new int[1000000];
Random random = new Random();


for (int i = 0; i < data.length; i++) {
data[i] = random.nextInt(1000000);
}
Arrays.parallelSort(data);
}
}
}
17 changes: 17 additions & 0 deletions green_pattern/patterns/Arrays_parallelsort/Before.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import java.util.Arrays;
import java.util.Random;

public class Before {

public static void main(String[] args) {
for (int k = 0 ; k < 1000 ; k++){
int[] data = new int[1000000];
Random random = new Random();

for (int i = 0; i < data.length; i++) {
data[i] = random.nextInt(1000000);
}
Arrays.sort(data);
}
}
}
Binary file added green_pattern/patterns/Arrays_parallelsort/after.jar
Binary file not shown.
Binary file not shown.
13 changes: 13 additions & 0 deletions green_pattern/patterns/Enhanced_for_Loop/After.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import java.util.Arrays;

public class After {
public static void main(String[] args) {
int[] array = new int[10000000];


for (int num : array) {
num = num * 2;
}

}
}
13 changes: 13 additions & 0 deletions green_pattern/patterns/Enhanced_for_Loop/Before.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import java.util.Arrays;

public class Before {
public static void main(String[] args) {
int[] array = new int[10000000];


for (int i = 0; i < array.length; i++) {
array[i] = array[i] * 2;
}

}
}
Binary file added green_pattern/patterns/Enhanced_for_Loop/after.jar
Binary file not shown.
Binary file added green_pattern/patterns/Enhanced_for_Loop/before.jar
Binary file not shown.
24 changes: 24 additions & 0 deletions green_pattern/patterns/For_optimization/After.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import java.util.stream.IntStream;


public class After {
public static void main(String[] args) {
long sum = 0;

for(int k = 0 ; k < 100 ; k++){
int[] arr = IntStream.range(1,100000000).toArray();
int len = arr.length;
int i;

for (i = 0; i < len - 10; i += 10) {
sum += arr[i] + arr[i + 1] + arr[i + 2] + arr[i + 3] + arr[i+4] + arr[i + 5] + arr[i + 6] + arr[i + 7] + arr[i+8] + arr[i + 9];
}

while (i < len) {
sum += arr[i];
i++;
}
}
System.out.println(sum);
}
}
19 changes: 19 additions & 0 deletions green_pattern/patterns/For_optimization/Before.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import java.util.stream.IntStream;


public class Before {
public static void main(String[] args) {
long sum = 0;

for(int k = 0 ; k < 100 ; k++){
int[] arr = IntStream.range(1,100000000).toArray();
int len = arr.length;
int i;

for (i = 0 ; i < len ; i++) {
sum += arr[i];
}
}
System.out.println(sum);
}
}
Binary file added green_pattern/patterns/For_optimization/after.jar
Binary file not shown.
Binary file added green_pattern/patterns/For_optimization/before.jar
Binary file not shown.
12 changes: 12 additions & 0 deletions green_pattern/patterns/Garbage_collect_optim/After.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
public class After {

public static void main(String[] args) {
for (int i = 0; i < 100000; i++) {

String myString2 = "Object " + i;
System.out.println(myString2);

myString2 = null;
}
}
}
9 changes: 9 additions & 0 deletions green_pattern/patterns/Garbage_collect_optim/Before.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
public class Before {
public static void main(String[] args) {
for (int i = 0; i < 100000; i++) {

String myString = new String("Object " + i);
System.out.println(myString);
}
}
}
Binary file added green_pattern/patterns/Garbage_collect_optim/after.jar
Binary file not shown.
Binary file not shown.
20 changes: 20 additions & 0 deletions green_pattern/patterns/HashMap_than_TreeMap/After.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import java.util.HashMap;
import java.util.Map;


public class After {
public static void main(String[] args) {

for (int i = 0 ; i < 100000 ; i++){
Map<String, Integer> hashMap = new HashMap<>();
hashMap.put("Alice", 25);
hashMap.put("Bob", 30);
hashMap.put("Eve", 22);


for (Map.Entry<String, Integer> entry : hashMap.entrySet()) {
System.out.println(entry.getKey() + ": " + entry.getValue());
}
}
}
}
18 changes: 18 additions & 0 deletions green_pattern/patterns/HashMap_than_TreeMap/Before.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import java.util.Map;
import java.util.TreeMap;

public class Before {
public static void main(String[] args) {

for (int i = 0 ; i < 100000 ; i++){
Map<String, Integer> treeMap = new TreeMap<>();
treeMap.put("Alice", 25);
treeMap.put("Bob", 30);
treeMap.put("Eve", 22);

for (Map.Entry<String, Integer> entry : treeMap.entrySet()) {
System.out.println(entry.getKey() + ": " + entry.getValue());
}
}
}
}
Binary file not shown.
Binary file not shown.
28 changes: 28 additions & 0 deletions green_pattern/patterns/Row-major_than_Column-major/After.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import java.util.Random;

public class After {
public static void main(String[] args) {
long totalSum = 0;
for (int p = 0 ; p < 1000 ; p++){
int rows = 1000;
int cols = 1000;
int[][] array = new int[rows][cols];
Random random = new Random();


for (int i = 0; i < rows; i++) {
for (int k = 0; k < cols; k++) {
array[i][k] = random.nextInt(100);
}
}

for (int i = 0; i < array.length; i++) {
int[] row = array[i];
for (int k = 0; k < row.length; k++) {
totalSum += row[k];
}
}
}
System.out.println(totalSum);
}
}
28 changes: 28 additions & 0 deletions green_pattern/patterns/Row-major_than_Column-major/Before.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import java.util.Random;

public class Before {
public static void main(String[] args) {
long totalSum = 0;
for (int p = 0 ; p < 1000 ; p++){
int rows = 1000;
int cols = 1000;
int[][] array = new int[rows][cols];
Random random = new Random();


for (int k = 0; k < cols; k++) {
for (int i = 0; i < rows; i++) {
array[i][k] = random.nextInt(100);
}
}

for (int k = 0; k < cols; k++) {
for (int i = 0 ; i < rows ; i++){
totalSum += array[i][k];
}

}
}
System.out.println(totalSum);
}
}
Binary file not shown.
Binary file not shown.
Loading

0 comments on commit 2ce7773

Please sign in to comment.