Last active
December 28, 2018 23:41
-
-
Save sayrer/428017592f739ca5ee444fc9998759d8 to your computer and use it in GitHub Desktop.
interviewcode
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#[derive(Clone, PartialEq, Hash, Eq)] | |
pub struct Entity { | |
start: usize, | |
end: usize, | |
html: String | |
} | |
impl Ord for Entity { | |
fn cmp(&self, other: &Self) -> Ordering { | |
self.start.cmp(&other.start) | |
} | |
} | |
impl PartialOrd for Entity { | |
fn partial_cmp(&self, other: &Self) -> Option<Ordering> { | |
Some(self.cmp(other)) | |
} | |
} | |
// ...snip... // | |
fn render_chars(text: &Vec<char>, entities: &mut Vec<Entity>) -> String { | |
let mut sb = String::with_capacity(text.len()*2); | |
entities.sort(); | |
let mut pos = 0 as usize; | |
for entity in entities { | |
for i in pos..entity.start { | |
sb.push(text[i]); | |
} | |
sb.push_str(&entity.html); | |
pos = entity.end; | |
} | |
for i in pos..text.len() { | |
sb.push(text[i]); | |
} | |
sb | |
} | |
/// Results | |
/* | |
Roberts-MacBook-Pro:interviewcode sayrer$ mvn test | |
------------------------------------------------------- | |
T E S T S | |
------------------------------------------------------- | |
Running interview.RendererBenchmarkTest | |
Classic: 976 ns/op | |
Classic: 845 ns/op | |
Classic: 788 ns/op | |
Classic: 831 ns/op | |
Classic: 796 ns/op | |
Memory: Classic: 1012 bytes/op | |
OptimizedClassic: 786 ns/op | |
OptimizedClassic: 550 ns/op | |
OptimizedClassic: 541 ns/op | |
OptimizedClassic: 546 ns/op | |
OptimizedClassic: 552 ns/op | |
Memory: OptimizedClassic: 279 bytes/op | |
OptimizedClassicWithCodePoints: 891 ns/op | |
OptimizedClassicWithCodePoints: 798 ns/op | |
OptimizedClassicWithCodePoints: 802 ns/op | |
OptimizedClassicWithCodePoints: 820 ns/op | |
OptimizedClassicWithCodePoints: 821 ns/op | |
Memory: OptimizedClassicWithCodePoints: 720 bytes/op | |
BinaryTree: 831 ns/op | |
BinaryTree: 727 ns/op | |
BinaryTree: 729 ns/op | |
BinaryTree: 727 ns/op | |
BinaryTree: 713 ns/op | |
Memory: BinaryTree: 935 bytes/op | |
LinkedListEntities: 903 ns/op | |
LinkedListEntities: 768 ns/op | |
LinkedListEntities: 770 ns/op | |
LinkedListEntities: 763 ns/op | |
LinkedListEntities: 757 ns/op | |
Memory: LinkedListEntities: 818 bytes/op | |
InsertionSort: 639 ns/op | |
InsertionSort: 626 ns/op | |
InsertionSort: 635 ns/op | |
InsertionSort: 606 ns/op | |
InsertionSort: 612 ns/op | |
Memory: InsertionSort: 714 bytes/op | |
BinarySearchSort: 732 ns/op | |
BinarySearchSort: 714 ns/op | |
BinarySearchSort: 689 ns/op | |
BinarySearchSort: 679 ns/op | |
BinarySearchSort: 688 ns/op | |
Memory: BinarySearchSort: 812 bytes/op | |
Tree: 769 ns/op | |
Tree: 749 ns/op | |
Tree: 708 ns/op | |
Tree: 705 ns/op | |
Tree: 701 ns/op | |
Memory: Tree: 861 bytes/op | |
StringReplacement: 1504 ns/op | |
StringReplacement: 1325 ns/op | |
StringReplacement: 1331 ns/op | |
StringReplacement: 1335 ns/op | |
StringReplacement: 1343 ns/op | |
Memory: StringReplacement: 4049 bytes/op | |
StringBuilderReplace: 620 ns/op | |
StringBuilderReplace: 558 ns/op | |
StringBuilderReplace: 570 ns/op | |
StringBuilderReplace: 564 ns/op | |
StringBuilderReplace: 562 ns/op | |
Memory: StringBuilderReplace: 415 bytes/op | |
StringBuilderReplaceWithCodePoints: 727 ns/op | |
StringBuilderReplaceWithCodePoints: 721 ns/op | |
StringBuilderReplaceWithCodePoints: 728 ns/op | |
StringBuilderReplaceWithCodePoints: 717 ns/op | |
StringBuilderReplaceWithCodePoints: 711 ns/op | |
Memory: StringBuilderReplaceWithCodePoints: 420 bytes/op | |
HashMapScan: 1249 ns/op | |
HashMapScan: 1189 ns/op | |
HashMapScan: 1194 ns/op | |
HashMapScan: 1197 ns/op | |
HashMapScan: 1214 ns/op | |
Memory: HashMapScan: 595 bytes/op | |
ArrayScan: 628 ns/op | |
ArrayScan: 623 ns/op | |
ArrayScan: 592 ns/op | |
ArrayScan: 597 ns/op | |
ArrayScan: 594 ns/op | |
Memory: ArrayScan: 803 bytes/op | |
Tests run: 13, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 76.436 sec | |
Running interview.RendererTest | |
Tests run: 10, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.001 sec | |
Results : | |
Tests run: 23, Failures: 0, Errors: 0, Skipped: 0 | |
[INFO] ------------------------------------------------------------------------ | |
[INFO] BUILD SUCCESS | |
[INFO] ------------------------------------------------------------------------ | |
[INFO] Total time: 01:17 min | |
[INFO] Finished at: 2018-12-28T15:39:38-08:00 | |
[INFO] ------------------------------------------------------------------------ | |
Roberts-MacBook-Pro:interviewcode sayrer$ cargo test | |
Finished dev [unoptimized + debuginfo] target(s) in 0.23s | |
Running target/debug/deps/interviewcode-e3b1fa7d042a6ba4 | |
running 4 tests | |
test rendertest::correctness ... ok | |
test rendertest::correctness_chars ... ok | |
test rendertest::bench_replacement ... ok | |
test rendertest::bench_replacement_chars ... ok | |
test result: ok. 4 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out | |
Roberts-MacBook-Pro:interviewcode sayrer$ cargo bench | |
Finished release [optimized + debuginfo] target(s) in 0.08s | |
Running target/release/deps/interviewcode-5095442a2564a737 | |
running 4 tests | |
test rendertest::correctness ... ignored | |
test rendertest::correctness_chars ... ignored | |
test rendertest::bench_replacement ... bench: 759 ns/iter (+/- 74) | |
test rendertest::bench_replacement_chars ... bench: 377 ns/iter (+/- 78) | |
test result: ok. 0 passed; 0 failed; 2 ignored; 2 measured; 0 filtered out | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment