ソースを参照

Timing now conditional. All faulty inputs detected.

themultiplexer 8年前
コミット
28178fdedd
2個のファイルの変更20行の追加11行の削除
  1. 0
    2
      hw7/task1/src/hash256.rs
  2. 20
    9
      hw7/task1/src/main.rs

+ 0
- 2
hw7/task1/src/hash256.rs ファイルの表示

@@ -14,8 +14,6 @@ pub fn verify_product(base: usize, number: usize, difficulty: &String) -> Option
14 14
 
15 15
     let hash = Sha256::hash(bytes).hex();
16 16
 
17
-    //println!("{} ends with {}", hash, difficulty);
18
-
19 17
     if hash.ends_with(difficulty) {
20 18
         return Some(Solution{ number: number, hash: hash,});
21 19
     }

+ 20
- 9
hw7/task1/src/main.rs ファイルの表示

@@ -13,33 +13,44 @@ pub fn main() {
13 13
     let diff = matches.value_of("difficulty").unwrap_or("1");
14 14
     let mut time_measurement = false;
15 15
 
16
-    println!("Using base: {}", base);
17
-    println!("Using difficulty: {}", diff);
18
-
19 16
     if diff.chars().any( |c| !c.is_digit(16)) {
20 17
         println!("Difficulty is not hexadecimal.");
21 18
         process::exit(1)
22 19
     }
23 20
 
24
-    if let Some(_) = matches.subcommand {
25
-        time_measurement = true;
21
+    if let Some(ref sub_command) = matches.subcommand {
22
+        if sub_command.name.eq("timings") {
23
+            time_measurement = true;
24
+        }
26 25
     }
27 26
 
28 27
     match base.parse::<usize>() {
29 28
         Ok(b) => {
29
+            println!("Using base: {}", base);
30
+            println!("Using difficulty: {}", diff);
30 31
             println!("Please wait...");
31 32
             let start = get_time();
32
-            for n in 0..<usize>::max_value() {
33
+
34
+            let max = <usize>::max_value();
35
+            for n in 0..max {
33 36
                 if let Some(x) = hash256::verify_product(b, n, &diff.to_string()) {
34 37
                     let end = get_time();
35
-                    let diff = end - start;
36 38
                     println!("Number: {} --> hash: {}", x.number, x.hash);
37
-                    println!("(Duration {}s / {}ms / {}us)", diff.num_seconds(), diff.num_milliseconds(), diff.num_microseconds().unwrap_or(0));
39
+                    if time_measurement{
40
+                        let diff = end - start;
41
+                        let s = diff.num_seconds();
42
+                        let ms = diff.num_milliseconds();
43
+                        let us = diff.num_microseconds().unwrap_or(ms * 1000);
44
+                        println!("(Duration {}s / {}ms / {}us)", s, ms, us);
45
+                    }
38 46
                     process::exit(0)
39 47
                 }
40 48
             }
41 49
         }
42
-        Err(_) => {}
50
+        Err(_) => {
51
+            println!("Base is not integer.");
52
+            process::exit(1)
53
+        }
43 54
     };
44 55
 }
45 56
 

読み込み中…
キャンセル
保存