speedup computation? - programming-languages

let's say i have an algorithm A run on two machines X and Y. So below is the result
Machine Elapsed time(sec)
X 20
Y 10
how do you express the speedup? ... thanks!

20 / 10 = 2, subtract 1 (base), multiply by 100 and you have a 100% speed-up on machine X.

Related

Google Kickstart Round D : Record Breaker Problem. Please hep me to debug my program

Problem
Isyana is given the number of visitors at her local theme park on N consecutive days. The number of visitors on the i-th day is Vi. A day is record breaking if it satisfies both of the following conditions:
The number of visitors on the day is strictly larger than the number of visitors on each of the previous days.
Either it is the last day, or the number of visitors on the day is strictly larger than the number of visitors on the following day.
Note that the very first day could be a record breaking day!
Please help Isyana find out the number of record breaking days.
Input
The first line of the input gives the number of test cases, T. T test cases follow. Each test case begins with a line containing the integer N. The second line contains N integers. The i-th integer is Vi.
Output
For each test case, output one line containing Case #x: y, where x is the test case number (starting from 1) and y is the number of record breaking days.
Limits
Time limit: 20 seconds per test set.
Memory limit: 1GB.
1 ≤ T ≤ 100.
0 ≤ Vi ≤ 2 × 105.
Test set 1
1 ≤ N ≤ 1000.
Test set 2
1 ≤ N ≤ 2 × 105 for at most 10 test cases.
For the remaining cases, 1 ≤ N ≤ 1000.
Sample
Input
Output
4
8
1 2 0 7 2 0 2 0
6
4 8 15 16 23 42
9
3 1 4 1 5 9 2 6 5
6
9 9 9 9 9 9
Case #1: 2
Case #2: 1
Case #3: 3
Case #4: 0
In Sample Case #1, the bold and underlined numbers in the following represent the record breaking days: 1 2 0 7 2 0 2 0.
In Sample Case #2, only the last day is a record breaking day.
In Sample Case #3, the first, the third, and the sixth days are record breaking days.
In Sample Case #4, there is no record breaking day.
-------------------------------------------------------------------------
This python code which i submitted in 'Kickstart Round D problem 1: Record Broker'. I executed this code on my local machine and there wasn't any run time error on top of that I couldn't brute force any test case that could break the code or give wrong answer. But while doing submission in kickstart it gives me runtime error. What could be the issue for getting run time error on kickstart? Please help!
cases = int(input().strip())
for q in range(1, cases + 1):
l = int(input().strip())
ls = list(map(int, input().split(" ")))
l = len(ls)
local_max = 0
count = 0
for i in range(l):
if i == 0:
if ls[1] < ls[0]:
local_max = ls[0]
count += 1
continue
if i == l - 1:
if ls[i] > ls[i - 1] and ls[i] > local_max:
count += 1
break
if ls[i] > ls[i - 1] and ls[i] > ls[i + 1] and ls[i] > local_max:
count += 1
local_max = ls[i]
continue
print("Case #{}: {}".format(q, count))
this line:
if ls[1] < ls[0]:
What if ls only has 1 element?
After fixing that your code still returns the wrong answer if there's only 1 element.
Also, consider this test case:
"65 87 87 34 64 59 93 20 95 85 24 99 62 100 60 19 100"
64 is not valid, but you count it.

Find the most similar vector/string in Matlab

Consider that I have some vectors (or strings) of numbers which generally have different length, eg x=[1 2 3 4 3 3 3 2 5].
Now, for a new vector y I want to find which one of the existing vectors x is the most similar.
Any idea?
The complete Problem:
I want to predict a time serie with some Neural Networks. Atevery step all the networks predict the next value of the serie. When the real value comes, the network that did the best prediction wins and I write its number to the vector X. After i finish with the timeserie1 i will have generate a vector X1 and each element of it will represend the best NN.
Now consider that i have 10 time series , so 10 X vectors. For a new one time serie Y i will do the same procedure. I want to define the kind of the Y using its similarity between this and the X vectors. I think the most importand aspect is the succession of the NN. I need for output something like a number or percentage of similarity.
eg:
X1= [ 1 1 2 2 3 3 4 4 5 5 6 6 ]
X2=[1 2 3 4 5 6 1 2 3 4 5 6]
Y=[1 1 1 2 2 3 4 5 5 6 6]
Then Y is more similar to X1

Why does Excel average gives different result?

Here's the table:
Should not they have the same result mathematically? (the average score of the per column and per row average)
The missing cells mean that your cells aren't all weighted evenly.
For example, row 11 has only two cells 82.67 and 90. So for your row average for row 11 they are weighted much more heavily than in your column averages where they are 1/13 and 1/14 of a column instead of 1/2 of a row.
Try filling up all the empty cells with 0 and the averages should match.
Taking a more extreme version of Ruslan Karaev's example:
5 5 5 | 5
1 | 1 Average of Average of Rows = (5 + 1 + 0) / 3 = 2
0 | 0
-----
2 5 5
Average of Average of Columns = (2 + 5 + 5) / 3 = 4
Yes, for example, the following two expressions:
/ a + b X + Y \ / a + X b + Y \
( ----- + ----- ) ( ----- + ----- )
\ 2 2 / \ 2 2 /
------------------- -------------------
2 2
are indeed mathematically equivalent, both coming out to be (a + b + X + Y) / 4.
However, short of having enough sufficient precision to store values, you may find that rounding errors accumulate differently depending on the order of operations.
You can see this sort of effect in a much simpler example if you assume a 3-digit precision and divide one by three, then multiply the result of that by three again:
1 / 3 -> 0.333, 0.333 x 3 -> 0.999
Contrast that with doing the operations in the oppisite order:
1 x 3 = 3, 3 / 1 = 1

Calculate bacteria number program in python

Bacteria B replicates itself each 2 minutes, write a program that asks users to enter two numbers: the initial B bacteria number and a period of time (in minutes). Calculate and print out the total number of B bacteria after this period.
How to do. I did like below but the result is wrong. Because bateria replicates itself each 2 minutes then I multiply it with time.
b = int(input('How many B Bacterias are there? '))
t = int(input('How much time will we waits (minutes)? '))
r = b * 2 * t
print ('After',t,'minutes(s) we would have',r,'B Bacterias')
See the image here
You have b bacterias at the t time like this:
t b
0 b
1 b
2 b * 2
3 b * 2
4 b * 4
5 b * 4
6 b * 8
7 b * 8
8 b * 16
and so on.
So your formula is r = b * 2 ** math.floor(t/2) and the code will be:
import math
b = int(input('How many B Bacterias are there? '))
t = int(input('How much time will we waits (minutes)? '))
r = b * 2 ** math.floor(t/2)
print ('After',t,'minutes(s) we would have',r,'B Bacterias')
Your function to calculate the number of bacteria is wrong, this is a textbook example of an exponential function.
should be r = b * 2 ** (t/2)
You need to take the time t and divide it by the division time of 2 minutes to get the number of divisions that occurred.
By raising 2 to this number you get the total offspring of one bacterial cell and this multiplied by the initial number of bacteria is your answer

How does a single CPU handle Multi-threaded and multi-process applications?

As I have read that for a multi-process application, a single CPU can handle only one task at a time, switching contexts between two processes. In a multi-threaded application, a single CPU can handle multiple threads. I do not understand this. Does the CPU handle one thread at a time if there is only one CPU? If yes, where is the advantage of having multi-threaded application vs multi-process application if CPU can handle one thing at a time.
TL;DR
Multithreading on a single core can speed up the application by using thread and instruction level parallelism.
If a single CPU has multiple cores it will run a process on each of the cores. If it does not, it will need to switch between processes on the single core.
Multithreading and multiprocessing can be combined for better results.
Full Explanation
Where multiprocessing systems include multiple complete processing units, multithreading aims to increase utilization of a single core by using thread-level as well as instruction-level parallelism. As the two techniques are complementary, they are sometimes combined in systems with multiple multithreading CPUs and in CPUs with multiple multithreading cores. Multithreading | WIkipedia
Example
A single CPU handles multi-threading in this way.
Let's say that we have two processes A and B which need to run a set of commands. After each command, the threads need the result. Here are the threads and the commands they need to run.
| # | Thread A | Thread B|
|---|----------|---------|
| 1 | 1 | 5 |
| 2 | 3 | 1 |
| 3 | Wait | 3 |
| 4 | 4 | 2 |
Now lets look at how the CPU would execute those (theoretically)
CPU Starts with thread A
CPU Pipeline
x x x x x x (1)
1 x x x x x (2) # Thread A, command 1 (1)
5 1 x x x x (3) # Thread B, command 1 (5)
3 5 1 x x x (4) # Thread A, command 2 (3)
1 3 5 1 x x (5) # Thread B, command 2 (1)
3 1 3 5 1 x (6) # Thread B, command 3 (3)... A is waiting for result of command 2
2 3 1 3 5 1 (7) # Thread B, command 4 (2)
x 2 3 1 3 5 (8)
x x 2 3 1 3 (9)
x x x 2 3 1 (10)
4 x x x 2 3 (11) # Thread A, command 4... A now has the result and can continue.
x 4 x x x 2 (12)
x x 4 x x x (13)
x x x 4 x x (14)
x x x x 4 x (15)
x x x x x 4 (16)
x x x x x x (17)
This is how that would look without multi threading.
CPU Pipeline
x x x x x x (1)
1 x x x x x (2) # Thread A, command 1 (1)
3 1 x x x x (3) # Thread A, command 2 (3)... A is waiting for result of command 2
x 3 1 x x x (4)
x x 3 1 x x (5)
x x x 3 1 x (6)
x x x x 3 1 (7)
x x x x x 3 (8)
4 x x x x x (9) # Thread A, command 4 (4)... A now has the result and can continue
x 4 x x x x (10)
x x 4 x x x (11)
x x x 4 x x (12)
x x x x 4 x (13)
x x x x x 4 (14)
5 x x x x x (15) # Thread B, command 1 (5)
1 5 x x x x (16) # Thread B, command 2 (1)
3 1 5 x x x (17) # Thread B, command 3 (3)
2 3 1 5 x x (18) # Thread B, command 4 (2)
x 2 3 1 5 x (19)
x x 2 3 1 5 (20)
x x x 2 3 1 (21)
x x x x 2 3 (22)
x x x x x 2 (23)
x x x x x x (24)
Thus with multithreading the threads would complete after 17 time steps, without it would take 24.
Questions?
Your core question is this: "[W]here is the advantage of having multi-threaded application vs multi-process application if CPU can handle one thing at a time?"
The simple answer is this -- threads share all memory and file descriptors automatically. If you use a multi-process application, you have to manually arrange to share memory or hand off file descriptors where needed. The relative advantages and disadvantages of multi-threading versus multi-process for parallelism are pretty much the same regardless of the number of cores present.
The advantage of multi-process is this same difference argued the other way around. Because processes don't share memory and file descriptors automatically, it's easier to keep things isolated when you don't want them shared.
From a practical standpoint, the answer is typically that the software needed to effectively create multi-process applications just doesn't exist yet. Whereas the software needed to create multi-threaded applications does. There's no good libraries in existence that permit you to have a pool of processes and dispatch tasks to the first available process. There no good way to have in-memory collections of various types that are shared across processes. In contrast, these kinds of tools are widely available for threads.
Yes one CPU can handle many processes or threads. It can do this by stopping one thread, and pausing it and starting another. There are several reasons you might want to do this. First of all many processes will spend some time waiting for external events (Such as a disk or database) while it is waiting then the CPU can work on something else.
Also back in the days when computers were much more expensive it allowed many people to share the use of one computer. When I started my CS Degree the department had one HP Unix server that everyone used. So I could be editing and compiling code, while another user was checking his email and 20 other people worked on this or that.
This has been in use in one form or another since the 60's.

Resources