Browse Source

Timing now conditional. All faulty inputs detected.

themultiplexer 8 years ago
parent
commit
28178fdedd
2 changed files with 20 additions and 11 deletions
  1. 0
    2
      hw7/task1/src/hash256.rs
  2. 20
    9
      hw7/task1/src/main.rs

+ 0
- 2
hw7/task1/src/hash256.rs View File

14
 
14
 
15
     let hash = Sha256::hash(bytes).hex();
15
     let hash = Sha256::hash(bytes).hex();
16
 
16
 
17
-    //println!("{} ends with {}", hash, difficulty);
18
-
19
     if hash.ends_with(difficulty) {
17
     if hash.ends_with(difficulty) {
20
         return Some(Solution{ number: number, hash: hash,});
18
         return Some(Solution{ number: number, hash: hash,});
21
     }
19
     }

+ 20
- 9
hw7/task1/src/main.rs View File

13
     let diff = matches.value_of("difficulty").unwrap_or("1");
13
     let diff = matches.value_of("difficulty").unwrap_or("1");
14
     let mut time_measurement = false;
14
     let mut time_measurement = false;
15
 
15
 
16
-    println!("Using base: {}", base);
17
-    println!("Using difficulty: {}", diff);
18
-
19
     if diff.chars().any( |c| !c.is_digit(16)) {
16
     if diff.chars().any( |c| !c.is_digit(16)) {
20
         println!("Difficulty is not hexadecimal.");
17
         println!("Difficulty is not hexadecimal.");
21
         process::exit(1)
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
     match base.parse::<usize>() {
27
     match base.parse::<usize>() {
29
         Ok(b) => {
28
         Ok(b) => {
29
+            println!("Using base: {}", base);
30
+            println!("Using difficulty: {}", diff);
30
             println!("Please wait...");
31
             println!("Please wait...");
31
             let start = get_time();
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
                 if let Some(x) = hash256::verify_product(b, n, &diff.to_string()) {
36
                 if let Some(x) = hash256::verify_product(b, n, &diff.to_string()) {
34
                     let end = get_time();
37
                     let end = get_time();
35
-                    let diff = end - start;
36
                     println!("Number: {} --> hash: {}", x.number, x.hash);
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
                     process::exit(0)
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
 

Loading…
Cancel
Save