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

L7 #403

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

L7 #403

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions Fibonacci.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import scala.concurrent.{Await, Future}
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.duration.Duration

object Fibonacci {

def fibonacci(n :Int): Int =
n match
case 1 => 1
case 2 => 1
case _ => fibonacci(n-1) + fibonacci(n-2)


def fibonacciParallel(n :Int, max: Int): Int =
fibonacciParallelSplit(n, 0, max)



def fibonacciParallelSplit(n :Int, current: Int, max: Int): Int =
if(current < max - 1) then
n match
case 1 => 1
case 2 => 1
case _ =>
//split
val fib1 = Future(fibonacciParallelSplit(n-1,current + 2, max) )
val fib2 = Future(fibonacciParallelSplit(n-2,current + 2,max) )

//wait for results
val result1 = Await.result(fib1, Duration.Inf)
val result2 = Await.result(fib2, Duration.Inf)

//return
result1 + result2
else
n match
case 1 => 1
case 2 => 1
case _ => fibonacci(n-1) + fibonacci(n-2)




}
95 changes: 95 additions & 0 deletions L7.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
import Fibonacci.*
import ListComparer.*
import QuickSort.*

import java.security.cert.PolicyQualifierInfo
import scala.concurrent.{Await, Future}
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.duration.Duration

def countTimeNs[T](task: => T): Long = {
val start = System.nanoTime();
task
val end = System.nanoTime()

(end - start)
}

def printTime(time: Long): Unit = {
val nano = time
val mili = time / 1000000

println(s"Mineło: ${nano} nanosekund, ${mili} milisekund")
println()
}


object L7 {
def main(args: Array[String]): Unit = {





print("Fibonacci: ")
printTime(countTimeNs(fibonacci(10)))
print("FibonacciParallel (6): ")
printTime(countTimeNs(fibonacciParallel(10,6)))
print("FibonacciParallel (18): ")
printTime(countTimeNs(fibonacciParallel(10,18)))
print("FibonacciParallel (2): ")
printTime(countTimeNs(fibonacciParallel(10,2)))


print("Fibonacci: ")
printTime(countTimeNs(fibonacci(45)))
print("FibonacciParallel (6): ")
printTime(countTimeNs(fibonacciParallel(45,6)))
print("FibonacciParallel (18): ")
printTime(countTimeNs(fibonacciParallel(45,18)))
print("FibonacciParallel (2): ")
printTime(countTimeNs(fibonacciParallel(45,2)))


println()
println()
println()

val listSmall1 = createList(1000,100_000)
val listSmall2 = createList(1000,100_000)


val listBig1 = createList(10,50_000_000)
val listBig2 = createList(10,50_000_000)

print("isSumGreater: ")
printTime(countTimeNs(isSumGreater(listSmall1,listSmall2)))
print("isSumGreaterParallel: ")
printTime(countTimeNs(isSumGreaterParallel(listSmall1,listSmall2)))

print("isSumGreater: ")
printTime(countTimeNs(isSumGreater(listBig1,listBig2)))
print("isSumGreaterParallel: ")
printTime(countTimeNs(isSumGreaterParallel(listBig1,listBig2)))

println()
println()
println()

val listSmall = List(1,2,3,-1,3,12,41,1,24,1,4,1,41,6,27,345,32)
val listBig = createList(1000, 50)

print("quicksort: ")
printTime(countTimeNs(quickSort(listSmall)))
print("quicksortParallel: ")
printTime(countTimeNs(quickSortParallel(listSmall)))


print("quicksort: ")
printTime(countTimeNs(quickSort(listBig)))
print("quicksortParallel: ")
printTime(countTimeNs(quickSortParallel(listBig)))


}
}
50 changes: 50 additions & 0 deletions ListComparer.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import scala.concurrent.duration.Duration
import scala.concurrent.{Await, Future}

object ListComparer {

def createList(maxNumber: Int, amount: Int):List[Int] = {
val random = scala.util.Random
val seq = for(i <- 0 to amount) yield random.nextInt(maxNumber)
seq.toList
}

def sum(list: List[Int]):Long =
def sumHelper(rest: List[Int], sum: Int):Long =
rest match
case Nil => sum
case _ => sumHelper(rest.tail, sum + rest.head)

sumHelper(list,0)





def isSumGreater(first: List[Int], second: List[Int]):Boolean = {
val firstSum = sum(first)
val secondSum = sum(second)

//println(s"${firstSum} ${secondSum}")


firstSum > secondSum
}


def isSumGreaterParallel(first: List[Int], second: List[Int]):Boolean = {
implicit val ec: scala.concurrent.ExecutionContext = scala.concurrent.ExecutionContext.global

val sumFirstFuture = Future(sum(first))
val sumSecondFuture = Future(sum(second))

val resultFirst = Await.result(sumFirstFuture, Duration.Inf)
val resultSecond = Await.result(sumSecondFuture, Duration.Inf)

//println(s"${resultFirst} ${resultSecond}")


resultFirst > resultSecond

}
}
28 changes: 28 additions & 0 deletions QuickSort.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import scala.concurrent.duration.Duration
import scala.concurrent.{Await, Future}

object QuickSort {
implicit val ec: scala.concurrent.ExecutionContext = scala.concurrent.ExecutionContext.global

def quickSort(list: List[Int]): List[Int] =
list match
case Nil => list
case el :: Nil => list
case pivot :: _ =>
val tail = list.tail
quickSort(tail.filter(i => i < pivot)) ::: list.filter(i => i == pivot) ::: quickSort(quickSort(tail.filter(i => i > pivot)))


def quickSortParallel(list: List[Int]): List[Int] =
list match
case Nil => list
case el :: Nil => list
case pivot :: _ =>
val tail = list.tail
val smallFuture = Future(quickSort(tail.filter(i => i < pivot)))
val bigFuture = Future(quickSort(tail.filter(i => i > pivot)))


Await.result(smallFuture, Duration.Inf) ::: list.filter(i => i == pivot) ::: Await.result(bigFuture, Duration.Inf)

}