03 dicembre 2018

/*
rifare 'tac' (legge da stdin e "ribalta") in due modi:

- ricorsivo
- iterativo

analizzare mem, tempistiche, espressività, etc.



Esiti performance (usare /usr/bin/time -v):
#iterativa:
real    0m2.002s
user    0m0.063s
sys     0m0.344s
User time (seconds): 0.01
        System time (seconds): 0.45
        Percent of CPU this job got: 24%
        Elapsed (wall clock) time (h:mm:ss or m:ss): 0:01.88
        Maximum resident set size (kbytes): 3872
        Minor (reclaiming a frame) page faults: 991
        Page size (bytes): 4096

#ricorsiva:
real    0m1.907s
user    0m0.063s
sys     0m0.359s
User time (seconds): 0.04
        System time (seconds): 0.26
        Percent of CPU this job got: 16%
        Elapsed (wall clock) time (h:mm:ss or m:ss): 0:01.85
        Maximum resident set size (kbytes): 7876
        Minor (reclaiming a frame) page faults: 1996
        Page size (bytes): 4096




*/

//Metodo iterativo:

/*
package main
import (
	"fmt"
	"os"
	"bufio"
)

func main() {
	var righe []string
	fmt.Println("Inserire il valore stop per interrompere la sequenza di inserimento:")
	scanner:=bufio.NewScanner(os.Stdin)

	// leggo file e riempio array
	for scanner.Scan(){
		riga:=scanner.Text()
		righe=append(righe,riga)
	}

	// stampo in senso inverso
	for j:=1;j<=len(righe);j++{
		fmt.Println(righe[len(righe)-j])
	}
}
*/
/*
package main

import (
	"fmt"
	"bufio"
	"os"
)

func main() {
	files := []string{}
	scanner := bufio.NewScanner(os.Stdin)

	for scanner.Scan() {
		tmp := scanner.Text()
		files = append(files, tmp)
	}

	fmt.Println("Iterative way: ")
	Iterative(files)

	fmt.Println("Recursion way: ")
	Recursion(files)
}

func Iterative(files []string) {
	for i := len(files) - 1; i >= 0; i-- {
		fmt.Println(files[i])
	}
}

func Recursion(files []string) {
	length := len(files) - 1

	if length >= 0 {
		fmt.Println(files[length])
		Recursion(files[:length])
	}
}
*/

package main

import (
    "fmt"
    "bufio"
    "os"
)

//var scanner *bufio.Scanner

func main() {
    scanner:=bufio.NewScanner(os.Stdin)
    recScan(scanner)
}

func recScan(scanner *bufio.Scanner) {
    if scanner.Scan() {
        riga := scanner.Text()
        recScan(scanner);
        fmt.Println(riga)
    }
}
package main

import (
    "fmt"
)

func main() {
    for i :=1 ; i < 1000; i++ {
        fmt.Println(fib(30))
    }
}

func fib(n int) int{
    fib_0, fib_1 := 1, 1
    for ; n >= 1; n-- {
        fib_0, fib_1 = fib_1, fib_0 + fib_1
    }
    return fib_0
}


/*
Command being timed: "go run Fib.go"
User time (seconds): 0.41
System time (seconds): 0.11
Percent of CPU this job got: 154%
Elapsed (wall clock) time (h:mm:ss or m:ss): 0:00.34
Average shared text size (kbytes): 0
Average unshared data size (kbytes): 0
Average stack size (kbytes): 0
Average total size (kbytes): 0
Maximum resident set size (kbytes): 34292
Average resident set size (kbytes): 0
Major (requiring I/O) page faults: 0
Minor (reclaiming a frame) page faults: 16750
Voluntary context switches: 1940
Involuntary context switches: 79
Swaps: 0
File system inputs: 0
File system outputs: 2384
Socket messages sent: 0
Socket messages received: 0
Signals delivered: 0
Page size (bytes): 4096
Exit status: 0
*/
package main

import (
    "fmt"
)

func main() {
    for i :=1 ; i < 1000; i++ {
        fmt.Println(fib(30))
    }
}

func fib(n int) int{
    if n < 2 {
        return 1
    }
    return fib(n-1) + fib(n-2)
}

/*
Command being timed: "go run RecFib.go"
User time (seconds): 7.79
System time (seconds): 0.11
Percent of CPU this job got: 102%
Elapsed (wall clock) time (h:mm:ss or m:ss): 0:07.70
Average shared text size (kbytes): 0
Average unshared data size (kbytes): 0
Average stack size (kbytes): 0
Average total size (kbytes): 0
Maximum resident set size (kbytes): 34356
Average resident set size (kbytes): 0
Major (requiring I/O) page faults: 0
Minor (reclaiming a frame) page faults: 19030
Voluntary context switches: 3009
Involuntary context switches: 102
Swaps: 0
File system inputs: 0
File system outputs: 2432
Socket messages sent: 0
Socket messages received: 0
Signals delivered: 0
Page size (bytes): 4096
Exit status: 0
*/
pub/labprog/20181203.txt · Last modified: 2018/12/10 16:16 by lucafavalli
CC Attribution-Share Alike 4.0 International
Driven by DokuWiki Recent changes RSS feed Valid CSS Valid XHTML 1.0