Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Submission : Golang #4

Open
Chibuzor-IN opened this issue Sep 12, 2018 · 0 comments
Open

Submission : Golang #4

Chibuzor-IN opened this issue Sep 12, 2018 · 0 comments

Comments

@Chibuzor-IN
Copy link

  • Time : ~940ms
  • Hardware :
Processor Name : Intel Core i7
Processor Speed : 2.8 GHz
Memory : 16 GB
  • Program :
package main

import (
	"fmt"
	"os"
	"bufio"
	"strconv"
	"time"
)

func readFile(filename string, separator func(data []byte, atEOF bool) (advance int, token []byte, err error), integerChan chan int, signalChan chan int){
	file, err := os.Open(filename)
	defer file.Close()

	if err != nil {
		fmt.Print(err)
	}

	scanner := bufio.NewScanner(file)

	const capacity = 100000*4
	buf := make([]byte, capacity)
	scanner.Buffer(buf, capacity)
	
	scanner.Split(separator)
	var sum int
	for scanner.Scan() {
		
		value, err := strconv.Atoi(scanner.Text())	
		if err == nil {						
			sum += value
		} else {			
		}	
	}	
	integerChan <- sum	
	signalChan <- 1
}

func main(){	
	start := time.Now()	
	separatorFunc := func(data []byte, atEOF bool) (advance int, token []byte, err error) {
		for i := 0; i < len(data); i++ {
			if data[i] == ',' || data[i] == '\r' || data[i] == '\n' {
				return i + 1, data[:i], nil
			}
		}		
		return 0, data, bufio.ErrFinalToken
	}

	integerChan, signalChan := make(chan int, 200001), make(chan int, 200001)
	for dirIndex := 1; dirIndex <= 991; dirIndex += 10 {
		var basePath string
		if dirIndex == 91 {
			basePath = fmt.Sprintf("files/0000%d-000%d/", dirIndex, dirIndex + 9)
		} else if dirIndex == 991 {
			basePath = fmt.Sprintf("files/000%d-00%d/", dirIndex, dirIndex + 9)
		} else if dirIndex > 91 {
			basePath = fmt.Sprintf("files/000%d-000%d/", dirIndex, dirIndex + 9)
		} else if dirIndex > 1 {
			basePath = fmt.Sprintf("files/0000%d-0000%d/", dirIndex, dirIndex + 9)
		} else {
			basePath = fmt.Sprintf("files/00000%d-0000%d/", dirIndex, dirIndex + 9)
		}

		for index := dirIndex; index <= dirIndex + 9; index++ {
		
			var path string
			if index >= 1000{
				path = fmt.Sprintf("%s00%d.csv", basePath, index)
			} else if index >= 100 {
				path = fmt.Sprintf("%s000%d.csv", basePath, index)
			} else if index >= 10 {
				path = fmt.Sprintf("%s0000%d.csv", basePath, index)
			} else {
				path = fmt.Sprintf("%s00000%d.csv", basePath, index)
			}			
			go readFile(path, separatorFunc, integerChan, signalChan)
		}
		
	}	

	var sum, signal int 
	for ; signal < 1000 ; signal += <- signalChan {
		sum += <- integerChan
	}	

	finished := time.Now()
	elapsed := finished.Sub(start)
	
	fmt.Printf("The sum is %d gotten after %s", sum, fmt.Sprint(elapsed))

	
}
@kayslay kayslay mentioned this issue Sep 24, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant