Ver código fonte

ADD.添加测试程序

RegMs If 4 anos atrás
pai
commit
dddb9bc6af
4 arquivos alterados com 34 adições e 13 exclusões
  1. 8 8
      go/parallel/main.go
  2. 1 1
      go/serial/main.go
  3. 5 4
      java/parallel/WordCount.java
  4. 20 0
      testcode.c

+ 8 - 8
go/parallel/main.go

@@ -163,7 +163,7 @@ func Map(text []byte, output chan Pair) {
 				word = word[:len(word)-1] // remove trailing quotation mark
 			}
 			if word != "" {
-				output <- Pair{word, 1}
+				output <- Pair{word, 1} // output pair
 				word = ""
 			}
 		}
@@ -172,7 +172,7 @@ func Map(text []byte, output chan Pair) {
 		word = word[:len(word)-1] // remove trailing quotation mark
 	}
 	if word != "" {
-		output <- Pair{word, 1}
+		output <- Pair{word, 1} // output pair
 	}
 	output <- Pair{"", 0} // map finish
 }
@@ -188,12 +188,12 @@ func Reduce(key string, list []int) Pair {
 
 func main() {
 	config := Config{
-		path:        "../testdata", // directory that stores data
-		mapCnt:      4,             // number of mapper
-		reduceCnt:   4,             // number of reducer
-		mapFunc:     Map,           // map function
-		combineFunc: Reduce,        // combine function
-		reduceFunc:  Reduce}        // reduce function
+		path:        "./testdata", // directory that stores data
+		mapCnt:      4,            // number of mappers
+		reduceCnt:   4,            // number of reducers
+		mapFunc:     Map,          // map function
+		combineFunc: Reduce,       // combine function
+		reduceFunc:  Reduce}       // reduce function
 	answer, err := Run(config)
 	if err != nil {
 		fmt.Println(err)

+ 1 - 1
go/serial/main.go

@@ -33,7 +33,7 @@ func (s PairSlice) Less(i, j int) bool {
 }
 
 func main() {
-	const path = "../testdata"
+	const path = "./testdata"
 	dataDir, err := os.ReadDir(path) // get directory info
 	if err != nil {
 		fmt.Println("Directory not found!")

+ 5 - 4
java/parallel/WordCount.java

@@ -32,7 +32,7 @@ public class WordCount {
                     }
                     if (str.length() > 0) {
                         word.set(str);
-                        context.write(word, one);
+                        context.write(word, one); // output pair
                         str = "";
                     }
                 }
@@ -42,7 +42,7 @@ public class WordCount {
             }
             if (str.length() > 0) {
                 word.set(str);
-                context.write(word, one);
+                context.write(word, one); // output pair
             }
         }
     }
@@ -58,7 +58,7 @@ public class WordCount {
             for (IntWritable val : values) {
                 sum += val.get();
             }
-            record.put(key.toString(), record.getOrDefault(key.toString(), 0) + sum); // update map, don't write context here
+            record.put(key.toString(), record.getOrDefault(key.toString(), 0) + sum); // update map, don't output here
         }
 
         /* cleanup function executed after reducing */
@@ -75,7 +75,7 @@ public class WordCount {
             for (HashMap.Entry<String, Integer> itr : list) {
                 word.set(itr.getKey());
                 result.set(itr.getValue());
-                context.write(word, result); // write context here
+                context.write(word, result); // output here
             }
         }
     }
@@ -85,6 +85,7 @@ public class WordCount {
         conf.set("mapreduce.output.textoutputformat.separator", " ");
         Job job = Job.getInstance(conf, "word count");
         job.setJarByClass(WordCount.class);
+        job.setNumReduceTasks(5); // number of reducers
         job.setMapperClass(TokenizerMapper.class); // map class
         job.setCombinerClass(IntSumReducer.class); // combine class
         job.setReducerClass(IntSumReducer.class); // reduce class

+ 20 - 0
testcode.c

@@ -0,0 +1,20 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <sys/time.h>
+
+int main() {
+    char cmd[100];
+    fgets(cmd, 100, stdin); // get command
+
+    struct timeval start, end;
+    long long total_time = 0, cnt = 100; // run 100 times
+    for (int i = 1; i <= cnt; i++) {
+        gettimeofday(&start, NULL);
+        system(cmd); // run command
+        gettimeofday(&end, NULL);
+        total_time += (end.tv_sec - start.tv_sec) * 1000000 + (end.tv_usec - start.tv_usec); // calculate total time
+        printf("%d-th time is %f s\n", i, (end.tv_sec - start.tv_sec) + (end.tv_usec - start.tv_usec) / 1000000.);
+    }
+    printf("average time is %f s\n", total_time / 1000000. / cnt);
+    return 0;
+}