Showing posts with label performance. Show all posts
Showing posts with label performance. Show all posts

Wednesday, 22 April 2020

Quiz on Textbook Sections 1.1 to 1.4, 1.6 to 1.9

Q1: Match each description with the class of computer. 
(i) General purpose, run a variety of software, subject to cost/performance tradeoff
Solution: Personal computers
(ii) Network based, high capacity, high performance, high reliability, range from small to building sized
Solution: Server computers
(iii) High-end scientific and engineering calculations, highest capability but represent a small fraction of the overall computer market
Solution: Supercomputers
(iv) Hidden as components of systems, stringent power/performance/cost constraints
Solution: Embedded computers

Q2: Which kind of computer can best be described as:
  • Battery operated 
  • Connects to the Internet 
  • Costs a few hundred dollars
  • Has touch screen
Solution: Personal mobile device




Q3: Which of the following are input devices? 
LCD display
Keyboard
Loudspeaker
Touchscreen
Pushbutton
Radio transmitter

Solution:
  1. Keyboard
  2. Touchscreen
  3. Pushbutton
Q4: Which of the following are output devices 
Loudspeaker
Temperature sensor
LED indicator light
Pushbutton
Mouse
LCD display 

Solution:
  1. Loudspeaker
  2. LED indicator light
  3. LCD display  
Q5: Match the following descriptions to the types of memory: 
(i) The storage area in which programs are kept when they are running and that contains the data needed by the running programs.
Solution: Main memory
(ii) Memory built as an integrated circuit; it provides random access to any location. Access times are 50 nanoseconds and cost per gigabyte in 2012 was $5 to $10.
Solution: Dynamic random access memory (DRAM)
(iii) A small, fast memory that acts as a buffer for a slower, larger memory.
Solution: Cache memory
(iv) Memory built as an integrated circuit, but faster and less dense than DRAM.
Solution: Static random access memory (SRAM)
(v) A form of nonvolatile secondary memory composed of rotating platters coated with a magnetic recording material. Because they are rotating mechanical devices, access times are about 5 to 20 milliseconds and cost per gigabyte in 2012 was $0.05 to $0.10.
Solution: Magnetic disk memory
(vi) A nonvolatile semi-conductor memory. It is cheaper and slower than DRAM but more expensive per bit and faster than magnetic disks. Access times are about 5 to 50 microseconds and cost per gigabyte in 2012 was $0.75 to $1.00.
Solution: Flash memory

Q6: Which of the following best defines the term "instruction set architecture"? 
Solution: An abstract interface between the hardware and the lowest-level software that encompasses all the information necessary to write a machine language program that will run correctly, including instructions, registers, memory access, I/O, and so on.

Q8: Computer C’s performance is 4 times as fast as the performance of computer B, which runs a given application in 28 seconds. How many seconds will computer C take to run that application? 
Solution: 7

Q9: A given application written in Java runs 15 seconds on a desktop processor. A new Java compiler is released that requires only 0.6 as many instructions as the old compiler. Unfortunately, it increases the CPI by 1.1. How fast can we expect the application to run using this new compiler? Pick the right answer from the three choices below: 
Solution: 15 × 0.6 × 1.1 = 9.9 seconds

 Q10: If we increase the clock frequency of a microprocessor, what will happen to the power consumption? 
Solution: Power will increase 

Saturday, 9 November 2019

Performance of the database

Q: A shop stores information on Customers(cid, name, address, last_visit), Inventory(iid, name, cost, price, stock) and Purchases( cid, iid, when, price, quantity) FK cid REF Customers(cid) ON DELETE RESTRICT ON UPDATE CASCADE, FK iid REF Inventory(iid) ON DELETE RESTRICT ON UPDATE CASCADE.
Which of the following actions are more likely than not to be true regarding the performance of the database? Select all that apply.

 
1. Using the CREATE INDEX statement to create an index to cid in Customers would make no difference at all.
2. Creating indexes on Purchases may be harmful if we are expecting very frequent INSERT operations.
3. Attribute iid of Inventory having an index is likely to assist with an INSERT operation on Customers.
4. Creating an index on the stock attribute of Inventory would be useful if changes are often due to customers purchasing items (the integer stored in stock decreases by the quantity of every purchase for the product with the iid given in the Purchases tuple), but there are few INSERT operations on the table.


Solution:
NOTE : The INSERT and UPDATE statements take more time on tables having indexes, whereas the SELECT statements become fast on those tables. The reason is that while doing insert or update, a database needs to insert or update the index values as well.
1. False
Explanation : Indexing in Customers is likely to improve its performance because it is less likely to have new customers very often so INSERT (or UPDATE) operations will be less on Customers. On the other hand, INSERT operations on Purchases is more likely and that would require lookup of cid in Customers table due to the presence of foreign key. So, indexing will decrease the lookup time in Customers table and hence, will increase the performance.
2. True
Explanation : As INSERT operations are expected to be more frequent, the indexing may slow down the operations and hence, may degrade the performance.
3. False
Explanation : INSERT operation on Customers has nothing to do with attribute iid of Inventory.
4. False
Explanation : Changes in stock attribute of Inventory due to customers purchasing items would be UPDATE operations. And UPDATE operations also take more time on tables having indexes and hence, may degrade the performance.
 

Sunday, 16 July 2017

Anatomy of a “Memory Leak”

Question: In .NET perspective:
  • What is a Memory Leak?
  • How can you determine whether your application leaks? What are the effects?
  • How can you prevent a memory leak?
  • If your application has memory leak, does it go away when the process exits or is killed? Or do memory leaks in your application affect other processes on the system even after process completion?
  • And what about unmanaged code accessed via COM Interop and/or P/Invoke?
I have some answers for these questions myself, but they are incomplete. What do you think?

Solution: The best explanation I've seen is in Chapter 7 of the free Foundations of Programming ebook.
Basically, in .NET a memory leak occurs when referenced objects are rooted and thus cannot be garbage collected. This occurs accidentally when you hold on to references beyond the intended scope.
You'll know that you have leaks when you start getting outofmemoryexceptions or your memory usage goes up beyond what you'd expect (perfmon has nice memory counters).
Understanding .NET's memory model is your best way of avoiding it. Specifically, understanding how the garbage collector works and how references work (again, I refer you to chapter 7 of the ebook). Also, be mindful of common pitfalls, probably the most common being events. If object A registered to an event on object B, then object A will stick around until object B disappears because B holds a reference to A. The solution is to unregister your events when you're done.
Of course, a good memory profile will let you see your object graphs and explore the nesting/referencing of your objects to see where references are coming from and what root object is responsible (red-gate ants profile, JetBrains dotMemory, memprofiler are really good choices, or you can use the text-only windbg and sos, but I'd strongly recommend a commercial/visual product unless you're a real guru).
I believe unmanaged code is subject to typical memory leaks of unamanged code, except that references shared between the two are managed by the garbage collector. Could be wrong about this last point.

Friday, 7 July 2017

What is the fastest way to get the value of π?

Question: I'm looking for the fastest way to obtain the value of π, as a personal challenge. More specifically I'm using ways that don't involve using #defined constants like M_PI, or hard-coding the number in.

Solution:

Bellard's formula, as used by PiHex, the now-completed distributed computing project, is used to calculate the nth digit of π in base 2. It is a faster version (about 43% faster) of the Bailey–Borwein–Plouffe formula.


/**
 * Prints the nth number of pi followed by the next 8 numbers in base 10.
 * This program is based on Bellard's work.
 */

public class Bpp {
 
 final static int NUM = 990; // nth number of pi to print out

 /**
  * Returns the nth digit of pi followed by the next 8 numbers
  * @param n - nth number of pi to return
  * @return returns an integer value containing 8 digits after n
  */
 public int getDecimal(long n) {
  long av, a, vmax, N, num, den, k, kq, kq2, t, v, s, i;
  double sum;

  N = (long) ((n + 20) * Math.log(10) / Math.log(2));

  sum = 0;

  for (a = 3; a <= (2 * N); a = nextPrime(a)) {

   vmax = (long) (Math.log(2 * N) / Math.log(a));
   av = 1;
   for (i = 0; i < vmax; i++)
    av = av * a;

   s = 0;
   num = 1;
   den = 1;
   v = 0;
   kq = 1;
   kq2 = 1;

   for (k = 1; k <= N; k++) {

    t = k;
    if (kq >= a) {
     do {
      t = t / a;
      v--;
     } while ((t % a) == 0);
     kq = 0;
    }
    kq++;
    num = mulMod(num, t, av);

    t = (2 * k - 1);
    if (kq2 >= a) {
     if (kq2 == a) {
      do {
       t = t / a;
       v++;
      } while ((t % a) == 0);
     }
     kq2 -= a;
    }
    den = mulMod(den, t, av);
    kq2 += 2;

    if (v > 0) {
     t = modInverse(den, av);
     t = mulMod(t, num, av);
     t = mulMod(t, k, av);
     for (i = v; i < vmax; i++)
      t = mulMod(t, a, av);
     s += t;
     if (s >= av)
      s -= av;
    }

   }

   t = powMod(10, n - 1, av);
   s = mulMod(s, t, av);
   sum = (sum + (double) s / (double) av) % 1;
  }
  return (int) (sum * 1e9); // 1e9 is 9 decimal places
 }

 private long mulMod(long a, long b, long m) {
  return (long) (a * b) % m;
 }

 private long modInverse(long a, long n) {
  long i = n, v = 0, d = 1;
  while (a > 0) {
   long t = i / a, x = a;
   a = i % x;
   i = x;
   x = d;
   d = v - t * x;
   v = x;
  }
  v %= n;
  if (v < 0)
   v = (v + n) % n;
  return v;
 }

 private long powMod(long a, long b, long m) {
  long tempo;
  if (b == 0)
   tempo = 1;
  else if (b == 1)
   tempo = a;

  else {
   long temp = powMod(a, b / 2, m);
   if (b % 2 == 0)
    tempo = (temp * temp) % m;
   else
    tempo = ((temp * temp) % m) * a % m;
  }
  return tempo;
 }

 private boolean isPrime(long n) {
  if (n == 2 || n == 3)
   return true;
  if (n % 2 == 0 || n % 3 == 0 || n < 2)
   return false;

  long sqrt = (long) Math.sqrt(n) + 1;

  for (long i = 6; i <= sqrt; i += 6) {
   if (n % (i - 1) == 0)
    return false;
   else if (n % (i + 1) == 0)
    return false;
  }
  return true;
 }

 private long nextPrime(long n) {
  if (n < 2)
   return 2;
  if (n == 9223372036854775783L) {
   System.err.println("Next prime number exceeds Long.MAX_VALUE: " + Long.MAX_VALUE);
   return -1;
  }
  for (long i = n + 1;; i++)
   if (isPrime(i))
    return i;
 }

 /**
  * Runs the program
  * @param args
  */
 public static void main(String args[]) {

  long duration = System.currentTimeMillis();

  Bpp bpp = new Bpp();
  System.out.println("Decimal digits of pi at position " + NUM + ": " + bpp.getDecimal(NUM) + "\n");

  duration = System.currentTimeMillis() - duration;
  System.out.println("> " + duration + " ms");
 }

}