mirror of https://github.com/sunface/rust-course
parent
c04cb28846
commit
6edaa7364a
@ -1 +1,3 @@
|
||||
# 优化编译速度
|
||||
|
||||
https://www.reddit.com/r/rust/comments/rnkyc0/why_does_my_code_compile_faster_on_nightly/
|
@ -1,2 +1,4 @@
|
||||
|
||||
https://www.reddit.com/r/rust/comments/rjumsg/any_good_resources_for_learning_rust_macros/
|
||||
https://www.reddit.com/r/rust/comments/rjumsg/any_good_resources_for_learning_rust_macros/
|
||||
|
||||
https://www.reddit.com/r/rust/comments/roaofg/procedural_macros_parsing_custom_syntax/
|
@ -0,0 +1,138 @@
|
||||
# 计算性能优化
|
||||
|
||||
|
||||
https://www.reddit.com/r/rust/comments/rn7ozz/find_perfect_number_comparison_go_java_rust/
|
||||
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math"
|
||||
"time"
|
||||
)
|
||||
|
||||
func main() {
|
||||
n := 320000
|
||||
nums := make(map[int][]int)
|
||||
start := time.Now()
|
||||
calPerfs(n, nums)
|
||||
fmt.Printf("runtime: %s\n", time.Since(start))
|
||||
}
|
||||
|
||||
func calPerfs(n int, nums map[int][]int) {
|
||||
for i := 1; i <= n; i++ {
|
||||
d := divs(i)
|
||||
if sum(d) == i {
|
||||
nums[i] = all(d)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func divs(num int) map[int]struct{} {
|
||||
r := make(map[int]struct{})
|
||||
r[1] = struct{}{}
|
||||
mid := int(math.Sqrt(float64(num)))
|
||||
for i := 2; i <= mid; i++ {
|
||||
if num%i == 0 {
|
||||
r[i] = struct{}{}
|
||||
r[num/i] = struct{}{}
|
||||
}
|
||||
}
|
||||
return r
|
||||
}
|
||||
|
||||
func sum(ds map[int]struct{}) int {
|
||||
var n int
|
||||
for k := range ds {
|
||||
n += k
|
||||
}
|
||||
return n
|
||||
}
|
||||
|
||||
func all(ds map[int]struct{}) []int {
|
||||
var a []int
|
||||
for k := range ds {
|
||||
a = append(a, k)
|
||||
}
|
||||
return a
|
||||
}
|
||||
```
|
||||
|
||||
## 120ms
|
||||
|
||||
```rust
|
||||
use std::time::Instant;
|
||||
|
||||
const N: usize = 320_000 ;
|
||||
|
||||
fn is_perfect(n: usize) -> bool {
|
||||
//println!("{:?}", n);
|
||||
let mut sum = 1;
|
||||
let end = (n as f64).sqrt() as usize;
|
||||
for i in 2..end + 1{
|
||||
if n % i == 0 {
|
||||
if i * i == n {
|
||||
sum += i;
|
||||
}
|
||||
else {
|
||||
sum += i + n / i;
|
||||
}
|
||||
}
|
||||
}
|
||||
sum == n
|
||||
}
|
||||
|
||||
fn find_perfs(n: usize) -> Vec<usize> {
|
||||
let mut perfs:Vec<usize> = vec![];
|
||||
for i in 2..n + 1 {
|
||||
if is_perfect(i) {
|
||||
perfs.push(i)
|
||||
}
|
||||
}
|
||||
perfs
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let start = Instant::now();
|
||||
let perfects = find_perfs(N);
|
||||
println!("{:?}", start.elapsed());
|
||||
println!("{:?}, in {:?}", perfects, N);
|
||||
}
|
||||
```
|
||||
|
||||
## 90ms
|
||||
|
||||
```rust
|
||||
use {
|
||||
std::{time::Instant},
|
||||
};
|
||||
|
||||
const N: usize = 320000;
|
||||
|
||||
// Optimized, takes about 320ms on an Core i7 6700 @ 3.4GHz
|
||||
fn cal_perfs2(n: usize) -> Vec<usize> {
|
||||
(1..=n)
|
||||
.into_iter()
|
||||
.filter(|i| cal2(*i) == *i)
|
||||
.collect::<Vec<_>>()
|
||||
}
|
||||
|
||||
fn cal2(n: usize) -> usize {
|
||||
(2..=(n as f64).sqrt() as usize)
|
||||
.into_iter()
|
||||
.filter_map(|i| if n % i == 0 { Some([i, n / i]) } else { None })
|
||||
.map(|a| a[0] + a[1])
|
||||
.sum::<usize>()
|
||||
+ 1
|
||||
}
|
||||
|
||||
|
||||
fn main() {
|
||||
let start = Instant::now();
|
||||
let perf2 = cal_perfs2(N);
|
||||
println!("{:?}",perf2);
|
||||
println!("Optimized: {:?}", start.elapsed());
|
||||
}
|
||||
```
|
@ -1,3 +1,5 @@
|
||||
# 最佳实践
|
||||
|
||||
https://www.reddit.com/r/rust/comments/rgjsbt/whats_your_top_rust_tip_crate_tool_other_for/
|
||||
https://www.reddit.com/r/rust/comments/rgjsbt/whats_your_top_rust_tip_crate_tool_other_for/
|
||||
|
||||
https://www.reddit.com/r/rust/comments/rnmmqz/question_how_to_keep_code_dry_when_many_similar/
|
@ -1,7 +0,0 @@
|
||||
一些关于println的技巧
|
||||
|
||||
## 打印对象地址
|
||||
```rust
|
||||
let v= vec![1,2,3];
|
||||
println!("{:p}",v.as_ptr())
|
||||
```
|
Loading…
Reference in new issue