Programming/JAVA, C++, Go, Rust(39)
-
Go에서 time의 ticker 사용
주기적으로 설정한 time tick 마다 깨어나서 동작하는 코드 peroid := 200 * time.Millisecond stop := make(chan struct{}, 1) go func() { ticker := time.NewTicker(period) defer ticker.Stop() for { select { case
2023.06.21 -
Go에서 CPU 사용량 측정
host machine의 CPU usage를 측정 import "github.com/shirou/gopsutil/cpu" period := 100 * time.Millisecond cpuInfo, err := cpu.Percent(period, false) if err != nil { return } c.usage = cpuInfo[0]container의 CPU usage를 측정 dat, err := ioutil.ReadFile(path) ... used, err := strconv.Atoi(strings.Replace(string(dat), "\n", "", 1)) ... time.Sleep(period) // process가 사용한 system time을 획득 var r syscall.Rusage s..
2023.06.21 -
Go 언어에서 reflect로 필드명과 값 획득 방법
func getKeyValueFromConfig(reflectValue reflect.Value, options *scenario.RequestOptions) { reflectType := reflectValue.Type() for i := 0; i < reflectValue.NumField(); i++ { field := reflectType.Field(i) value := reflectValue.Field(i).Interface() fmt.Printf("Field: %s, Value: %v\n", field.Name, value) options.InputOptions[field.Name] = value } }
2023.06.20 -
고 언어 (Go lang)에서 값의 스왑 (swap) 방법
Go lang에서 값을 swap 하는 방법에 대해 살펴보겠습니다. 간단하게 다음의 문제를 풀면서 swap을 적용해 보겠습니다. nums = [3, 2, 1, 4] 위와같은 배열이 있습니다. 배열의 값 1은 배열의 좌측끝으로, 배열 내 값 4는 배열의 우측 끝으로 이동시키고자 합니다. 이때 이동 방법은 인접한 두 개의 값을 서로 swap 하는 방법으로만 이동 할 수 있습니다. 다음과 같이 위 알고리즘 문제를 해결하면서 swap을 적용해 볼 수 있습니다. func swap(val1 *int, val2 *int) { temp := *val1 *val1 = *val2 *val2 = temp } func semiOrderedPermutation(nums []int) int { i := 0 one_idx := -1..
2023.06.05 -
Kotlin build on Mac
1. install brew install kotlin2. compile test.kt fun main() { println("test"); }kotlinc-jvm test.kt -include-runtime -d test.jar3. execute java -jar test.jar
2023.02.27 -
C++ inline variable
inline variable ? C++17 C++17부터 inline variable을 지원합니다. 변수 선언 시 inline keyword를 지정해주면, 해당 변수를 include 하여 사용하는 쪽에 embed 되어 사용됩니다. 그런데, 이렇게 되면 여러 파일이 하나의 inline 변수를 include 하여 사용할 경우 여러 변수들이 여러 파일에 중복으로 정의되어 사용되는 것이 아닌가 할 수도 있습니다. C++에서의 header란 것이 그러니깐요. 그러나 C++17 부터 새롭게 추가된 inline 변수는 linker level에서 여러 변수의 정의를 하나로 통합해 주는 작업을 수행합니다. 당연히 이를 참조하는 여로곳 에서는 변수를 extern으로 접근하게 되죠. 다음과 같이 선언하여 사용할 수 있습니다..
2023.02.09