Twitter Goodies

String Interning

Strings in C# are highly optimized but also potentially very wasteful. The most important thing to know about the String object in .NET is that is immutable.That is, once created, a string can never get longer, get shorter, or have any of its characters changed. If you perform a lot of string manipulations, you end up creating a lot of String objects on the heap, which cause more frequent garbage collections, thus hurting your application’s performance. If you need to perform a lot of string manipulation efficiently, the StringBuilder class is your choice.

However there are a few tricks to write efficient code. For example, if you have several instances of the same string duplicated in memory, you’re wasting memory because strings are immutable. You’ll use memory much more efficiently if there is just one instance of the string in memory and all variables needing to refer to the string can just point to the single string object.

String interning is a mechanism that reuses memory storage for strings if their contents are the same. If a string containing “foo” is loaded twice, the CLR can decide to allocate the string once and return the same reference to both users. The CLR automatically stores all string literals declared in an application in an area of memory called the intern pool. The intern pool contains a unique instance of each string literal found in your code, which allows for more efficient use of resources by not storing multiple copies of strings that contain the same string literal.

The Intern and IsInterned instance method of the string class allow you to use the intern pool. Below you make use of Intern method:

 

string s1 = "Luca A. Tarrini";
string s2 = new StringBuilder().Append("Luca A.").Append(" Tarrini").ToString();
string s3 = String.Intern(s2);
Console.WriteLine((object)s1 == (object)s2);    //Different reference
Console.WriteLine((object)s3 == (object)s1);    //Same reference

String objects referred to by the internal pool can’t be freed until the AppDomain is unloaded or the process terminates.

Luca A. Tarrini


Scoped Pointer

One of the problems of working with raw pointers which allocate memory on the heap is that is easy to forget to delete them. Another problem is that is quite easy to reassign the pointer in the code to some other object and cause a memory leak since the reference to the original object is lost. Also, exceptions can cause a failure to reach the delete command. Finally, it is quite hard to keep track of the object’s life time, in particular if the object is passed to the other objects and functions. It is hard to say if the object is still referenced by some other object or if it can be deleted at a certain point. Modern language as Java or C# have garbage collector which takes care of all problems above. In C++, this problems can be handled by smart pointers.

The principle of smart pointers is based on a idiom to ensure that dynamically allocated memory is released properly under all circumstances, freeing the developer from the burden of managing this on her own. This guarantee is accomplished by initializing a smart pointer with the address of a dynamically allocated object which in turn is used to release the memory during destruction. Since the destructor is always executed the contained memory is therefore always released.

A scoped pointer is a pointer that is the sole owner of a dynamically allocated object. The corresponding class is named boost::scoped_ptr and is defined in boost/scoped_ptr.hpp. A scoped pointer is not able to transfer ownership of its contained object to another scoped pointer. Once initialized with an address, the dynamically allocated object is released during destruction. A scoped_ptr assumes ownership of the resource to which it points, and never accidentally surrenders that ownership.

It is a lightweight smart pointer; using it doesn’t make your program larger or run slower. It only makes your code safer and more maintainable.

#include<boost/scoped_ptr.hpp>
#include<string>
#include<iostream>

int main()
{
    boost::scoped_ptr<std::string> p(new std::string("Luca A. Tarrini"));
    if(p)
        std::cout << *p << std::endl;

    size_t i = p->size();
    *p = "Elisa Tarrini";
    std::cout << *p << std::endl;
}

Note how there’s no call to delete, as the scoped_ptr is an automatic variable and is therefore destroyed as it goes out of scope.

Luca A. Tarrini


Pointers in a Vector

A vector container makes a copy of the objects you add to it. This could be very inconvenient if your objects are large. There could be considerable overhead in copying each object as you add it to the container. Give a look below

vector<Person *> people;
.
.
people.push_back(new Person(firstname, secondname));
.
.
auto iter(people.begin());
while(iter != people.end())
    (*(iter++))->showPerson();

iter = people.begin();
while(iter != people.end())
    delete *(iter++);

I create a vector <T> template type parameter of Person*. Each Person object is now created on the heap, and the address is passed to the push_back() function for the vector. With objects created on the heap using the new operator, the objects are only destroyed when you remove them using delete. Then using the loop, I will delete each of Person objects.

Luca A. Tarrini


Cephei.QL :: C++/CLI wrapper around QuantLib for F#

I found this library Cephei.QL is a library that wraps the QuantLib C++ library with classes that provide CTS interfaces that can be used from F#.

Luca A. Tarrini


HOW TO BE A QUANT AND GET A QUANT CAREER

I found this post on Quant job for quant newbies from Bryan Downing. The post is a collection of resources how to be a Quant. I point out to what software you need for beginner.

Matlab for prototyping. Most banks and large hedge funds use this but R is definitely on the rise but I know Matlab is widely used in industry. For example, I know that Blackrock specifically uses Octave which is a Matlab clone. C++ is without doubt the number one programming language in quant. You should never worry about what others say, this language is the kingpin throughout industry. Java is big and so is C# but if there is one language you need to know, it is C++! Python is getting bigger too, but focus on C++ if you never programmed before. The C++ libraries and APIs that are heavily sought after are STL (Standard Template Library), some Boost, but there is one never widely talked about. Try to learn QuantLib.

Many firms have their own APIs for quant related platforms but this is the easiest way to get familiar on how this works. It is somewhat large as it was developed in C++ so that will give you a better handle on how to master the typical development life cycle at large investment firms. QuantLib also has a front end Excel plugin (QuantLibXL) as well which can be useful as many traders like VBA. You would be able to see how Excel would communicate with a large quant based C++ API like QuantLib. That skill alone would give you a major leg up against other candidates. You can also use other languages as well to communicate with QuantLib. A good place to start for understanding High Frequency Trading Platforms is Marketcetera.

Luca A. Tarrini


error lnk1104: cannot open file quantlib-vc100-mt-gd.lib

Solution for error lnk1104: cannot open file quantlib-vc100-mt-gd.lib. Note that very often, users don’t setup a project correctly since they build QuantLib in one mode and try to call it from a test program which runs in some other mode. If you built QuantLib in release mode, you should build your project in release mode. If you build your project in debug mode without ever building QuantLib in debug mode, you’ll have link errors.

Luca A.Tarrini


WinObj v2.22

Every release of Windows introduces new object types with Windows 7 defining a total of 42. You can see the objects defined by running the Sysinternals Winobj utility with administrative rights and navigating to the ObjectTypes directory in the Object Manager namespace.

Luca A. Tarrini


Should I learn Silverlight? Objective C? HTML 5?

I finished to read a Rockford Lhotka’s post about Should I learn Silverlight? Objective C? HTML 5?. I suggest to read it. From the crystal ball of Lhokta, we can infer .NET and XAML remain entirely valid, and life generally continues like it is today, with a mix of .NET smart client work and primarily server-based web work with h5/js primarily used to boost the user experience, but not often used to write standalone smart client apps. To summarize, I think learning XAML is time well spent. Today that’s WPF or Silverlight. There is absolutely no doubt that Silverlight is closer to WinRT than WPF, and people building SL apps today will have an easier time migrating them to WinRT later, whereas most WPF apps will be a pretty big rewrite.

Luca A. Tarrini


The Heart of Derivative Modeling

Reading my new book, the preface started so….Derivative modeling is at the heart of quantitative research and development on Wall Street. Practitioners (i.e., Wall Street trading desk quants) and academics alike spend much research, money, and time developing efficient models for pricing, hedging, and trading equity and fixed income derivatives. Many of these models involve complicated algorithms and numerical methods that require lots of computational power.

Luca A. Tarrini


Winsock <-> Berkeley Sockets API

Winsock API was developed as an extension of the Berkeley Sockets API into the Windows environment, and all Windows versions support Winsock. The original include file for use with Windows Sockets 1.1 was the Winsock.h header file.

To ease porting existing source code based on Berkeley UNIX sockets to Windows sockets, Windows Sockets development kits for Winsock 1.1 were encouraged to be supplied with several include files with the same names as standard UNIX include files (the sys/socket.h and arpa/inet.h header files, for example).

When Windows Sockets 2 was released, the primary include file for use with Windows Sockets was renamed to Winsock2.h. The older original Winsock.h header file for Winsock 1.1 was also retained for compatibility with older applications. The development of Winsock 1.1 compatible applications has been deprecated since Windows 2000 was released. All applications should now use use the include Winsock2.h directive in Winsock application source files.

Give a reading here.

Luca A. Tarrini